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 bottommostC 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 (S1and 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
分析:
洗牌。两摞牌数相同。每个字母代表一个颜色,给出两摞牌的顺序,最后一行为洗牌目标。
![](/qrcode.jpg)
利用bfs搜索。每摞最后一个为s2的,最上面的是s1的,如果新的序列s与目标序列相同,返回洗牌次数,如果不相同,将序列s重新分成两摞,下半部分是s1的,上半部分是s2的。由此进行。
每次洗牌需进行查重。防止进入循环状态而无解。
代码:
#include<iostream> #include<queue> #include<stdio.h> #include<map> using namespace std; int n,m,t; string s1,s2,s; struct node{ int cnt;//计数 string s; }; map<string,int>p; int bfs(node a) { queue<node>q; p.clear(); //删除所有元素 q.push(a);//从已有元素后增加元素 while(!q.empty()){//判断是否为空 node u=q.front();//把第一个元素给u q.pop();//清除第一个元素 if(u.s==s&&u.cnt!=0) return u.cnt; node v; int i; for(i=0;i<n;i++)//洗牌穿插 { v.s += u.s[n+i]; v.s +=u.s[i]; } if(!p.count(v.s)){//判断是否为目标顺序 p[v.s]=1;//标记。证明其不会有重复的顺序 v.cnt=u.cnt+1; q.push(v); } } return -1; } int main() { int k=0; scanf("%d",&t); while(t--) { cin>>n>>s1>>s2>>s; node u; u.s=s1+s2; u.cnt=0; int ans=bfs(u); printf("%d %d\n",++k,ans); } }