您的位置:首页 > 其它

Shuffle'm Up(模拟)

2016-03-24 13:47 309 查看
I - Shuffle'm UpCrawling in process...Crawling failedTime Limit:1000MS    Memory Limit:65536KB     64bit IO Format:%I64d & %I64uSubmitStatusPracticePOJ 3087Appoint description:System Crawler (2016-03-17)DescriptionA 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 containingC 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 forC = 5:The single resultant stack, S12, contains 2 *C chips. The bottommost chip of S12 is the bottommost chip fromS2. On top of that chip, is the bottommost chip fromS1. The interleaving process continues taking the 2nd chip from the bottom ofS2 and placing that onS12, followed by the 2nd chip from the bottom ofS1 and so on until the topmost chip fromS1 is placed on top ofS12.After the shuffle operation, S12 is split into 2 new stacks by taking the bottommostC chips from S12 to form a newS1 and the topmostC chips from S12 to form a newS2. 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 stackS12 can be formed by shuffling two stacks some number of times.InputThe 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 integerC, (1 ≤ C ≤ 100) which is the number of chips in each initial stack (S1 andS2). 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 theC 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.OutputOutput for each dataset consists of a single line that displays the dataset number (1 thoughN), 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
题意:
已知两堆木片s1和s2的初始状态,其木片数均为c,按给定规则能将他们相互交叉组合成一堆木片s12,再将s12的最底下的c块木片归为s1,最顶的c块木片归为s2,依此循环下去。问经过多少次新的组合之后,s12的状态和目标状态des相同,若永远不可能相同,则输出"-1"。
AC代码:
#include<iostream>#include<algorithm>#include<cstring>#include<string>#include<cstdio>#include<set>using namespace std;#define T 8805#define mod 10007typedef long long ll;int n,m,c;struct node{int i;char s1[T],s2[T],s[T];void count(){for(i=0;i<m;i++){s[i<<1] = s2[i];}for(i=0;i<m;++i){s[i<<1|1] = s1[i];}s[2*m] = '\0';}void slip(){for(i=0;i<2*m;++i){if(i<m){s1[i] = s[i];}else{s2[i-m] = s[i];}}s1[m] = '\0';s2[m] = '\0';}}a;bool jugde(char* str){set<string> v;set<string>::iterator it;while(true){c++;a.count();if(strcmp(a.s,str)==0){return true;}it = v.find(a.s);if(it!=v.end())return false;a.slip();v.insert(a.s);}return false;}int main(){#ifdef zscfreopen("input.txt","r",stdin);#endifint cnt=0;bool flag;char str[T];scanf("%d",&n);while(n--){scanf("%d",&m);scanf("%s",&a.s1);scanf("%s",&a.s2);scanf("%s",&str);c = 0;flag = jugde(str);if(flag){printf("%d %d\n",++cnt,c);}else{printf("%d -1\n",++cnt);}}return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  模拟