题目链接:http://poj.org/problem?id=3087

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 C chips. 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:

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.

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 * C uppercase 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.

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.

Sample Input

2
4
AHAH
HAHA
HHAAAAHH
3
CDE
CDE
EEDDCC

Sample Output

1 2
2 -1

题意:

给出两堆同样数量的筹码 $S_1$ 和 $S_2$。洗筹码的方式是,将 $S_1$ 和 $S_2$ 互相间隔插入,同时 $S_1$ 靠上,$S_2$ 靠下。

然后将新生成的筹码堆的下面一半作为新的 $S_1$,上面一半作为新的 $S_2$。不断循环如上操作,问是否能达到题目所给的目标结果。

题解:

这个在kuangbin的专题里放进了搜索里面,然而感觉每次洗筹码生成的结果都是唯一的,所以不就是一直洗牌直到能达到目标为止吗?感觉并不是搜索啊……

洗牌如果洗了一个循环,发现并不能达到目标的话,就是Impossible了。

AC代码:

#include<cstdio>
#include<iostream>
#include<string>
using namespace std;
typedef pair<string,string> pss; int n;
pss st,ed;
void nxt(pss &x)
{
string s;
for(int i=;i<n;i++) s+=x.second[i], s+=x.first[i];
x.first=s.substr(,n);
x.second=s.substr(n,n);
}
int main()
{
int T;
cin>>T;
for(int kase=;kase<=T;kase++)
{
scanf("%d",&n);
cin>>st.first>>st.second;
cin>>ed.first;
ed.second=ed.first.substr(n,n);
ed.first.erase(n,n);
int cnt;
pss now;
for(cnt=,now=st;;cnt++,nxt(now))
{
if(now==st && cnt>)
{
printf("%d -1\n",kase);
break;
}
if(now==ed)
{
printf("%d %d\n",kase,cnt);
break;
}
}
}
}

POJ 3078 - Shuffle'm Up - [模拟题]的更多相关文章

  1. poj 1008:Maya Calendar(模拟题,玛雅日历转换)

    Maya Calendar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 64795   Accepted: 19978 D ...

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

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

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

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

  4. poj 1028 Web Navigation 【模拟题】

    题目地址:http://poj.org/problem?id=1028 测试样例: Sample Input VISIT http://acm.ashland.edu/ VISIT http://ac ...

  5. POJ 3087 Shuffle'm Up 模拟,看着不像搜索啊

    题意:给定s1,s1两副扑克,顺序从下到上.依次将s2,s1的扑克一张一张混合.例如s1,ABC; s2,DEF. 则第一次混合后为DAEBFC. 然后令前半段为s1, 后半段为s2. 如果可以变换成 ...

  6. poj 1573 Robot Motion【模拟题 写个while循环一直到机器人跳出来】

                                                                                                         ...

  7. POJ 3393 Lucky and Good Months by Gregorian Calendar 模拟题

    题目:http://poj.org/problem?id=3393 不多说了,简单模拟题,因为粗心写错了两个字母,导致错了N遍,模拟还是一贯的恶心,代码实在不想优化了,写的难看了点.. #includ ...

  8. POJ 模拟题集合

    http://www.cppblog.com/Uriel/articles/101592.html 感觉这个暑假没有去年有激情啊,,,还没到状态就已经块上学了,,, 真是弱暴了,,,找几道模拟题刷刷. ...

  9. POJ 3923 Ugly Windows(——考察思维缜密性的模拟题)

    题目链接: http://poj.org/problem?id=3923 题意描述: 输入一个n*m的屏幕 该屏幕内有至少一个对话框(每个对话框都有对应的字母表示) 判断并输出该屏幕内处于最表层的对话 ...

随机推荐

  1. PowerShell 显示气球提示框 1

    #加载 Winform 程序集,使用Out-Null抑制输出 [system.Reflection.Assembly]::LoadWithPartialName('System.Windows.For ...

  2. 使用 TRESTClient 与 TRESTRequest 作为 HTTP Client(转)

    使用 TRESTClient 与 TRESTRequest 作为 HTTP Client 转自:http://www.cnblogs.com/dennieschang/p/6966403.html   ...

  3. asp.net中 使用参数化mysqlparameter 保存数据时,总保存成一个汉字的解决方案。

    var param = new MySqlParameter("@" + columName, property.Value); param.DbType = DbType.Str ...

  4. python中字符串前的r什么意思

    Python中,u表示unicode string,表示使用unicode进行编码,没有u表示byte string,类型是str,在没有声明编码方式时,默认ASCI编码.如果要指定编码方式,可在文件 ...

  5. MobaXterm v10.9破解

    去官网下载个人版 Exeinfo查壳发现无壳 载入OD,右键,字符串智能搜索. Ctrl+F搜索关键词About,找到到FormAbout处,即关于窗体的创建和显示的位置.双击查看汇编代码 程序在窗体 ...

  6. [转]Emmet使用详解

    Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生.它使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度,而且作为一款插件能 ...

  7. Pycharm 在Windows下出现闪退问题(即是在运行一段时间后,自己就退出崩掉了)的解决方法

    Pycharm 在Windows下出现闪退问题(即是在运行一段时间后,自己就退出崩掉了)的解决方法 最近自己下载了最新版本的Pycharm,运行程序过程中发现,在运行一段时间后(比如10几分钟),Py ...

  8. MongoDB GridFS 存储文件

    使用MongoDB的GridFS方式. CSDN: https://blog.csdn.net/qq_32657967/article/details/81534259官方文档: https://do ...

  9. 【C语言】 重拾

    [C语言] 因为以前学过C语言,只不过太长时间不用,已经忘得差不多了… 所以这篇文章的性质是把C语言中一些对于现在的我不是很符合预期的知识点记录一下. ■ HelloWorld程序 HelloWorl ...

  10. python一个简单的打包例子

    最近写了一些工具,想到分享给同事时好麻烦,并且自己每次用也是需要打开pycharm这些工具,感觉很麻烦,因此想到打包,网上有些例子,照做后又摸索很久方成,索性记录一下,以备不时之需. 主要参考:htt ...