POJ 3087 Shuffle'm Up(洗牌)

Time Limit: 1000MS    Memory Limit: 65536K

Description - 题目描述

  A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of poker chips, S1 and S2, each stack containing Cchips. Each stack may contain chips of several different colors.

  The actual shuffle operation is performed by interleaving a chip from S1 with a chip from S2 as shown below for C = 5:

洗筹码是玩家在扑克牌桌上的常见消遣。开始时有两堆筹码S1和S2,每堆C片。每堆筹码都有可能包含几种不同的颜色。

实际操作为将S1的筹码片不断交错在S2上,如下图C=5时:

CN

  The single resultant stack, S12, contains 2 * C chips. The bottommost chip of S12 is the bottommost chip from S2. On top of that chip, is the bottommost chip from S1. The interleaving process continues taking the 2nd chip from the bottom of S2 and placing that on S12, followed by the 2nd chip from the bottom of S1 and so on until the topmost chip from S1 is placed on top of S12.

  After the shuffle operation, S12 is split into 2 new stacks by taking the bottommost C chips from S12 to form a new S1 and the topmost C chips from S12 to form a new S2. The shuffle operation may then be repeated to form a new S12.

  For this problem, you will write a program to determine if a particular resultant stack S12 can be formed by shuffling two stacks some number of times.

唯一产生的筹码堆S12,有2*C片。S12的底部筹码片是S2的底部筹码。再上面的一片筹码片,即是S1的底部筹码。交错处理将不断重复从S2底部取下一片筹码放到S12上,再从S1底部取下一片筹码,直到S1的顶部筹码被放在S12上。

混洗后,S12分为两堆新筹码。底下的C片筹码成为新的S1,上面的C片筹码成为新的S2。随后还能混洗出新的S12。

对于这个问题,你需要写个程序来确定某种S12是否可以通过混洗若干次两堆筹码得到结果。

CN

Input - 输入

  The first line of input contains a single integer N, (1 ≤ N ≤ 1000) which is the number of datasets that follow.

  Each dataset consists of four lines of input. The first line of a dataset specifies an integer C, (1 ≤ C ≤ 100) which is the number of chips in each initial stack (S1 and S2). The second line of each dataset specifies the colors of each of the C chips in stack S1, starting with the bottommost chip. The third line of each dataset specifies the colors of each of the C chips in stack S2 starting with the bottommost chip. Colors are expressed as a single uppercase letter (A through H). There are no blanks or separators between the chip colors. The fourth line of each dataset contains 2 * Cuppercase letters (A through H), representing the colors of the desired result of the shuffling of S1 and S2 zero or more times. The bottommost chip’s color is specified first.

输入的第一行是一个整数N( ≤ N ≤ ),表示随后测试用例的数量。

每个测试用例有四行。
第一行有一个整数C( ≤ C ≤ ),表示每个初始堆(S1 和 S2)的筹码片数。
第二行表示堆S1中C片筹码的各个颜色,顺序自底向上。
第三行表示堆S2中C片筹码的各个颜色,顺序依然自底向上。
颜色以单个大写字母(A 到 H)表示。筹码片颜色间没有空格。
第四行有2*C个大小字母(A 到 H),表示S1与S2混洗若干次后的目标结果。顺序还是自底向上。

CN

Output - 输出

  Output for each dataset consists of a single line that displays the dataset number (1 though N), a space, and an integer value which is the minimum number of shuffle operations required to get the desired resultant stack. If the desired result can not be reached using the input for the dataset, display the value negative 1 (−1) for the number of shuffle operations.

对于每个测试用例,输出用例编号( 到 N),一个空格,一个得到目标结果的最小混洗数。如果无法混洗出目标结果,则输出-。

CN

Sample Input - 输入样例

2
4
AHAH
HAHA
HHAAAAHH
3
CDE
CDE
EEDDCC

Sample Output - 输出样例

1 2
2 -1

题解

  按着搜索的分类滚进这道题的,然而怎么看都不像搜索……(水平太渣?)
  感觉应该存在规律,就按照样例写了写……
  先把S1和S2整合在一起,下标从0开始,观察下标变动。
  小于C的一半坐标变动为i -> i*2+1
  另一半坐标变动为 i -> (i-c)*2
  打个坐标变动的表,模拟一下就出来了。

代码 C++

 #include <cstdio>
#include <string>
#define mx 205
int main(){
std::string fid, tmp;
int t, n, c, i, ed, nxtTable[mx], nxt[mx], opt;
char data[mx], nxtData[mx]; for (t = scanf("%d", &n); t <= n; ++t){
scanf("%d ", &c);
gets(gets(data) + c);
fid = gets(nxtData);
for (i = , ed = c << ; i < ed; ++i){
if (i < c) nxtTable[i] = i << | ;
else nxtTable[i] = (i - c) << ;
nxtData[nxt[i] = nxtTable[i]] = data[i];
}
for (opt = ;; ++opt){
if ((tmp = nxtData) == fid || nxt[] == ) break;
for (i = ; i < ed; ++i) nxtData[nxt[i] = nxtTable[nxt[i]]] = data[i];
}
if (tmp == fid) printf("%d %d\n", t, opt);
else printf("%d -1\n", t);
}
return ;
}

最新文章

  1. Constraint6:更新外键约束(Foreign Key Constraint)的引用列
  2. mysql 有两种数据库引擎发音
  3. iOS9适配 之 关于info.plist 第三方登录 添加URL Schemes白名单
  4. C# 跨线程操作无效
  5. ASP.NET中GridView控件删除数据的两种方法
  6. 桶排序(bucket sort)
  7. [King.yue]EXT.Grid行双击事件
  8. CentOS 7 之安装Mono&amp;MonoDevelop
  9. Poweroff – 很好很强大的定制关机工具
  10. Struts2 学习笔记 10 Result部分 part1
  11. html 5 废弃的标签和属性
  12. Android中如何查找内存泄露
  13. python===基础,修改idle的输入风格
  14. 手把手教你整合SSM框架(基于课工厂+MyEclipse 2017 CI 10)
  15. DecimalFormat格式化十进制数字
  16. unbuntu系统( PC机 )中安装360wifi步骤
  17. windows 活动目录双向信任配置
  18. vue构建项目全过程
  19. 对于src路径问题,深层理解的实践。且对于输出流write()两个方法的源码阅读。
  20. 【数据库】悲观锁与乐观锁与MySQL的MVCC实现简述

热门文章

  1. python pandas使用数据透视表
  2. redis常见应用场景
  3. Hive中如何快速的复制一张分区表(包括数据)
  4. python中函数嵌套、函数作为变量以及闭包的原理
  5. c# ref和out参数
  6. PIVOT(透视转换)和UNPIVOT(逆透视转换)
  7. window下nodejs用nodemon启动koa2项目(用cmd启动不了,要用Git Bash Here 启动才可以)
  8. Spring IOC 和 AOP
  9. 使用WebClient下载网页,用正则匹配需要的内容
  10. 删去k个数字后的最小值