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 ;
}

POJ 3087 Shuffle'm Up(洗牌)的更多相关文章

  1. POJ.3087 Shuffle'm Up (模拟)

    POJ.3087 Shuffle'm Up (模拟) 题意分析 给定两个长度为len的字符串s1和s2, 接着给出一个长度为len*2的字符串s12. 将字符串s1和s2通过一定的变换变成s12,找到 ...

  2. DFS POJ 3087 Shuffle'm Up

    题目传送门 /* 题意:两块扑克牌按照顺序叠起来后,把下半部分给第一块,上半部给第二块,一直持续下去,直到叠成指定的样子 DFS:直接模拟搜索,用map记录该字符串是否被搜过.读懂题目是关键. */ ...

  3. POJ 3087 Shuffle'm Up

    Shuffle'm Up Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  4. poj 3087 Shuffle'm Up ( map 模拟 )

    题目:http://poj.org/problem?id=3087 题意:已知两堆牌s1和s2的初始状态, 其牌数均为c,按给定规则能将他们相互交叉组合成一堆牌s12,再将s12的最底下的c块牌归为s ...

  5. POJ 3087 Shuffle'm Up (模拟+map)

    题目链接:http://poj.org/problem?id=3087 题目大意:已知两堆牌s1和s2的初始状态, 其牌数均为c,按给定规则能将他们相互交叉组合成一堆牌s12,再将s12的最底下的c块 ...

  6. [暴力搜索] POJ 3087 Shuffle'm Up

    Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10003   Accepted: 4631 Des ...

  7. (简单) POJ 3087 Shuffle'm Up,枚举。

    Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuff ...

  8. poj 3087 Shuffle'm Up (模拟过程)

    Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuff ...

  9. POJ 3087 Shuffle'm Up bfs

    题目链接:Shuffle'm Up 除了英文题有点恶心.发现模拟 + bfs 就可以过的时候,就是水了. 一个bug 就是filp函数得到string s12失败了.恩.据大腿告知,string 并不 ...

随机推荐

  1. Java Redis JNI

    基本参考菜鸟教程,java下载直接安装,注意文件名和类名需要一致的问题: redis下载以后按菜鸟教程linux下安装,方式编译运行ok: Java使用redis按菜鸟教程下载.jar,保存在本地某个 ...

  2. yum 原理C/S原理结构图

    yum 原理C/S原理结构图

  3. 量化交易-外汇交易-MetaTrader5

    量化交易-外汇交易-MetaTrader5 外汇有充足的流动性, 7*24, 交易成本低,多空双向,外加杠杆,无人能控盘,有模拟盘,相当适合做量化交易练习积累经验. 第一,全球最大最公平的市场.外汇市 ...

  4. NUL和NULL

    此处讨论C语言中的NUL和NULL 1.NUL是ASCII字符集中'\0'字符的名字,它的字节模式为全0 2.NULL指一个其值为0的指针 3.它们都是整型值,其值也相同,所以可以互换使用 4.然而, ...

  5. 笔面试复习(spring常用.jar包/事务/控制反转/bean对象管理和创建/springMVC工作原理/sql查询)

    ###spring常用jar包1.spring.jar是包含有完整发布模块的单个jar包.2.org.springframework.aop包含在应用中使用Spring的AOP特性时所需要的类.3.o ...

  6. Python 内置函数sorted()在高级用法

    对于Python内置函数sorted(),先拿来跟list(列表)中的成员函数list.sort()进行下对比.在本质上,list的排序和内建函数sorted的排序是差不多的,连参数都基本上是一样的. ...

  7. Docker学习笔记之常用的 Docker Compose 配置项

    0x00 概述 与 Dockerfile 一样,编写 Docker Compose 的配置文件是掌握和使用好 Docker Compose 的前提.编写 Docker Compose 配置文件,其本质 ...

  8. 【题解】Luogu P2319 [HNOI2006]超级英雄

    原题传送门 这道题就是一个很简单的二分图匹配 二分图匹配详解 一开始想的是2-sat和网络流,根本没想匈牙利和HK 这道题只要注意一点:当一个点匹配不成功之后就直接退出 剩下的就写个二分图最大匹配就行 ...

  9. Linux-eval

    shell中eval的用法示例: 语 法:eval [参数] 功能说明:eval会对后面的[参数]进行两遍扫描,如果在第一遍扫面后cmdLine是一个普通命令,则执行此命令:如果cmdLine中含有变 ...

  10. BZOJ 1232 安慰奶牛题解

    题目传送门:BZOJ 1232 这是一个边权和点权结合在一起的题,但是因为要从当前点出发并回到原点,所以每个边都被经过了两次,节点至少被经过一次,所以我们将边权重新赋值,所以推出 那么遍历之后,并不是 ...