Shuffle'm Up
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15249   Accepted: 6962

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

Source

题意:先给你两个长度一样的初始状态字符串s1,s2和一个大小为两倍的最终状态的s12,按照s2的第一个先放在s12第一个再依次是s1,s2,s1…,排完后再按照长度前一半是s1新状态,后一半是s2新状态, 重复以上操作,看得到s12是否有和 最初给的最终状态的s12相同的,有输出步数,没有就输出-1.

思路:按照题目的顺序模拟即可,可以将每一个整合后的一个新的S12放进一个map里面,接下去如果模拟到了目标串就输出,如果发现模拟到的一个s12在map中出现过了并且不是和目标串的一样的话,那么这个时候出现了一个循环,无论如何也模拟不出目标串了,就可以输出-1了。注意那个'\0',一开始没有加这个wa了好几发。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
#define _e exp(1.0)
#define ll long long
const int maxn=1e6+; char a[maxn];
char b[maxn];
char c[maxn];
char goal[maxn];
int main()
{
int t;
scanf("%d",&t);
int ca=;
while(t--)
{
int n;
int step=;
scanf("%d",&n);
scanf("%s%s",a,b);
scanf("%s",goal);
map<string,int>mp;
mp[goal]=;
while()
{ int pos=;
for(int i=;i<n;i++)
{
c[pos++]=b[i];
c[pos++]=a[i];
}
c[pos]='\0';
step++;
// for(int i=0;i<2*n;i++)
// cout<<c[i];
if(strcmp(c,goal)==)
{
cout<<ca++<<" "<<step<<endl;
break;
}
else if(mp[c]== && strcmp(goal,c))
{
cout<<ca++<<" "<<-<<endl;
break;
}
mp[c] = ;
int ii,k;
for(ii=;ii<n;ii++) //分拆出s1与s2
a[ii]=c[ii];
a[ii]='\0'; for(k=;ii<*n;ii++,k++)
b[k]=c[ii];
b[ii]='\0'; }
}
}

Shuffle'm Up POJ - 3087(模拟)的更多相关文章

  1. POJ - 3087 模拟 [kuangbin带你飞]专题一

    模拟洗牌的过程,合并两堆拍的方式:使先取s2,再取s1:分离成两堆的方式:下面C张放到s1,上面C张到s2.当前牌型与第一次相同时,说明不能搜索到答案. AC代码 #include<cstdio ...

  2. kuangbin专题 专题一 简单搜索 Shuffle'm Up POJ - 3087

    题意:(1)有两副颜色多样的扑克牌,(A~H)表示不同颜色,给你两副牌,S1,S2和一副你需要洗出的KEY,S12由S2最底部,S1底部...一直下去,直到洗成S12,就是图片展示的那样.(2)洗好的 ...

  3. POJ 3087 模拟

    给定两个长度为len的字符串s1和s2, 接着给出一个长度为len*2的字符串s12. 将字符串s1和s2通过一定的变换变成s12,找到变换次数 变换规则如下: 假设s1=12345,s2=67890 ...

  4. POJ 3087 模拟+hash

    也可以用map来搞 样例推出来 就没啥问题了 (先读的是B 然后是A 被坑好久) //By SiriusRen #include <cstdio> #include <iostrea ...

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

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

  6. 【模拟】POJ 3087

    直达–>POJ 3087 Shuffle'm Up 题意:一开始没怎么看明白,注意现是从S2里拿牌放在最底下,再放S1,这样交叉放(我一开始以为是S1和S2随意哪个先放,分别模拟取最小),然后在 ...

  7. POJ 3087 Shuffle'm Up

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

  8. POJ 3087 Shuffle'm Up(洗牌)

    POJ 3087 Shuffle'm Up(洗牌) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 A common pas ...

  9. DFS POJ 3087 Shuffle'm Up

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

随机推荐

  1. Bootstrap插件-collapse

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...

  2. Java笔记--动态代理

    Java动态代理 1.概念 代理: 有时我们并不想直接访问对象A,或者不能直接访问对象A.而是通过访问一个中间对象B,让中间对象B去访问A.这种方式就称为代理. 这里的对象A所属的类就为委托类,或者被 ...

  3. 转载 tomcat6下项目移植到tomcat7下出问题的解决办法

    转载,原文地址  http://hw1287789687.iteye.com/blog/1817865 org.apache.catalina.core.ContainerBase addChildI ...

  4. 删除elasticsearch大于7天前的索引

    curl -u 用户名:密码 -H'Content-Type:application/json' -d'{ "query": { "range": { &quo ...

  5. 查看mysql表大小

    //先进去MySQL自带管理库:information_schema //自己的数据库:dbwww58com_kuchecarlib //自己的表:t_carmodelparamvalue mysql ...

  6. leetcode——2

    1. 题目 Add Two Numbers You are given two linked lists representing two non-negative numbers. The digi ...

  7. 往ABAP gateway system上和Cloud Foundry上部署HTML5应用

    ABAP Gateway system 在我的公众号文章里有详细介绍:SAP Fiori应用的三种部署方式 用WebIDE部署 用Eclipse Team provider部署 执行report /U ...

  8. Python基础总结与实践

    Python简介 Python是一种动态解释型编程语言,在模块载入时将源码编译成字节码, 这些字节码被虚拟机PVM解释执行,其中解释执行是Python性能较低的主要原因: Python使用C语言编写, ...

  9. POJ 1742 Coins(多重背包,优化)

    <挑战程序设计竞赛>上DP的一道习题. 很裸的多重背包.下面对比一下方法,倍增,优化定义,单调队列. 一开始我写的倍增,把C[i]分解成小于C[i]的2^x和一个余数r. dp[i][j] ...

  10. python 数据库操作 SQLite、MySQL 摘录

    转自: http://www.cnblogs.com/windlaughing/p/3157531.html 不管使用什么后台数据库,代码所遵循的过程都是一样的:连接 -> 创建游标 -> ...