Shuffle'm Up
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 90 Accepted Submission(s) : 34
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.
2 4 AHAH HAHA HHAAAAHH 3 CDE CDE EEDDCC
1 2 2 -1题意:给出字符串的长度,吧s1和s2混合,混合的方式按照图2,问几次可以得到s3,混合后的s按照图3的方式拆开前半s1 后半s2思路:乍一看以为是深搜(可是都说是bfs),好吧,写了个递归,(代码贴最下面)也就错了十几次吧,最后放弃了,直接按模拟题做提交了到后来我终于发现了我的错误,是因为在temp数组里存的上一个的字符串的东西没有清除,也就是如果比较的第一组数据比第二组长,上一组的数据会混淆测试组,如果自己写一个比较的函数,检查是否两个字符串是否相等应该可以,比较紧,pass了这个,就模拟吧
- Source Code
#include<iostream> #include<stdio.h> #include<string.h> #include<map> #include<string> using namespace std; char s1[201],s2[201],s3[500],s[500]; int main() { int t,len,ans; int i,j,k; cin>>t; for(i=1;i<=t;i++) { cin>>len; cin>>s1>>s2>>s3; map<string,int>q; q[s3]=1; ans=0; while(1) { int t=0; for(j=0;j<=len; j++) { s[t++]=s2[j]; s[t++]=s1[j]; } ans++; if(strcmp(s,s3)==0) { cout<<i<<" "<<ans<<endl; break; } if(q[s]==1) { cout<<i<<" -1"<<endl; break; } q[s]=1; for(k=0; k<len; k++) { s1[k]=s[k]; s2[k]=s[k+len]; } } } return 0; }
/*#include<iostream>
#include<string>
#include<string.h>
#include<stdio.h>
using namespace std;
const int maxn=10001;
char temp[maxn],ss1[maxn],ss2[maxn];
char temp1[maxn],temp2[maxn];
char s1[maxn],s2[maxn],s3[maxn];
int len;
void ble(char s1[],char s2[])
{
int i,k=0;
for(i=0;i<len;i++)
{
temp[k++]=s2[i];
temp[k++]=s1[i];
}
}
void tap(char s[])
{
int i,j=0;
for(i=0;i<len;i++)
temp1[i]=s[i];
for(i=len;i<2*len;i++)
{
temp2[j]=s[i];
j++;
}
}
/*int dfs(int step)
{
ble(temp1,temp2);
//printf("%s\n",temp);
if(strcmp(temp,s3)==0)
{
//printf("%s\n",temp);
return step;
}
tap(temp);
if(strcmp(temp1,s1)==0&&strcmp(temp2,s2)==0)
return -1;
return dfs(step+1);
}*/
int main()
{
int t;
int i,j=0;
int ans;
cin>>t;
while(t--)
{
cin>>len;
ans=0;
scanf("%s %s %s",&s1,&s2,&s3);
strcpy(temp1,s1);
strcpy(temp2,s2);
int gg=1;
ble(temp1,temp2);
if(strcmp(temp,s3)==0)
ans=1;
else {
tap(temp);
for(ans=1;;ans++)
{
ble(temp1,temp2);
if(strcmp(s1,temp1)==0&&strcmp(s2,temp2)==0)
{
ans=-1;
break;
}
gg=2;
if(strcmp(temp,s3)==0)
{ans++;
break;
}
tap(temp);
}
}
//if(strcmp(s1,s2)==0)
//cout<<999999<<endl;
//printf("%s",temp1);
//ble(s1,s2);
//for(i=0;i<2*len;i++)
//tap(s3);
//printf("%s %s",ss1,ss2);
//int ans=dfs(1);
j++;
cout<<j<<" "<<ans<<endl;
}
return 0;
}*/