poj3087 Shuffle'm Up

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 newS12.

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 theC 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 throughH). 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

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <string>
 5 #include <queue>
 6 #include <map>
 7 using namespace std;
 8 int C;
 9 string str1,str2,str3;
10 queue<char>q1;
11 queue<char>q2;
12 map<string,int>my_map;
13 void moni()
14 {
15     while(!q1.empty())
16     q1.pop();
17     while(!q2.empty())
18     q2.pop();
19     my_map.clear();
20     int i;
21     int cnt=0;
22     bool flag=false;
23     string my_str;
24     while(1)
25     {
26         cnt++;
27         for(i=0;i<C;i++)
28         {
29             q1.push(str1[i]);
30             q2.push(str2[i]);
31         }
32         my_str.clear();
33         for(i=0;i<C;i++)
34         {
35             my_str+=q2.front();
36             q2.pop();
37             my_str+=q1.front();
38             q1.pop();
39         }
40         if(!my_map[my_str])
41         {
42             my_map[my_str]++;
43         }
44         else
45         {
46             flag=true;
47             break;
48         }
49         if(my_str==str3)
50         {
51             printf("%d\n",cnt);
52             break;
53         }
54         str1="";
55         str2="";
56         for(i=0;i<C;i++)
57         {
58             str1+=my_str[i];
59         }
60         for(i=C;i<2*C;i++)
61         {
62             str2+=my_str[i];
63         }
64     }
65     if(flag)
66     {
67         printf("-1\n");
68     }
69 }
70 int main()
71 {
72     int t;
73     scanf("%d",&t);
74     int cnt=1;
75     while(t--)
76     {
77         scanf("%d",&C);
78         cin>>str1>>str2>>str3;
79         printf("%d ",cnt++);
80         moni();
81     }
82     return 0;
83 }
View Code

此题说实话,就一个模拟题。重在逻辑。

题目大意是:给两堆牌S1和S2(ABC和BCA),注意给的每一堆牌的顺序是从下到上,先拿S2这一堆的最底下的那张牌即B,再拿S1的最底下A,以此类推,得到S12(BACBAC),

这个S12的顺序也是从下到上的顺序,然后最底下C张变成原来的S1,最顶上C张变成S2,重复操作,判断到某一个S12 状态需要操作的次数。

然后就是纯模拟,我运用了队列和map来实现,map主要是判重,一旦出现重复,意外着出现了循环,就永远到达不了目标状态,即输出-1;

posted @ 2013-06-01 15:31  欧阳生朵  阅读(1051)  评论(0编辑  收藏  举报