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 EEDDCCSample 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;}
相关文章推荐
- Transformation 能将 Windows XP/Server 2003 操作系统,完美地模拟成 Windows Vista 的软件
- 用javascript和css模拟select的脚本
- PHP模拟asp.net的StringBuilder类实现方法
- C#使用ImitateLogin模拟登录百度
- javascript用层模拟可移动的小窗口
- 自编jQuery插件实现模拟alert和confirm
- JS模拟按钮点击功能的方法
- PHP模拟asp中response类实现方法
- 模拟Spring的简单实现
- javascript 模拟点击广告
- JQuery中模拟image的ajaxPrefilter与ajaxTransport处理
- php实现模拟post请求用法实例
- JavaScript实现MIPS乘法模拟的方法
- 模拟xcopy的函数
- JS模拟实现Select效果代码
- php模拟服务器实现autoindex效果的方法
- C# SendInput 模拟鼠标操作的实现方法
- PHP模拟登陆163邮箱发邮件及获取通讯录列表的方法
- js实现模拟银行卡账号输入显示效果
- JS模拟并美化的表单控件完整实例