Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9277 | Accepted: 4256 |
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 f(指定)irst.
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#include<stdio.h> #include<map> #include<string> #include<string.h> using namespace std; int main() { int t; int n; char s1[1000],s2[1000],s12[1000],ss[1000]; scanf("%d",&t); int cou=0; while(t--) { cou++; int flag=0; scanf("%d",&n); map<string,bool>vis; scanf("%s%s%s",s1,s2,ss); vis[ss]=1; int step=0; while(1) { step++; int c=0; for(int i=0;i<n;i++) { s12[c++]=s2[i]; s12[c++]=s1[i]; } s12[c]='\0'; if(strcmp(s12,ss)==0) { flag=1; //printf("%d\n",step); break; } if(vis[s12]==1) { //printf("-1\n"); break; } vis[s12]=1; for(int i=0;i<n;i++) { s1[i]=s12[i]; } c=0; for(int i=n;i<2*n;i++) { s2[c++]=s12[i]; } s2[c]='\0'; } if(flag==1) printf("%d %d\n",cou,step); else printf("%d -1\n",cou); } return 0; }