题意:
s1, s2两堆拍,交叉洗牌组成s12(自下而上,最底下的一张是s2的第一张)
给出s1, s2, s12,问s1和s2是否可以经过多次洗牌后达到s12的状态
思路:
简单模拟,在找到最终状态前,洗牌时若出现重复,则说明形成了环,无法达到想要的结果
代码如下:
#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
#include<string>
using namespace std;
#define INF 1e7+9
typedef long long ll;
const int N = 1010;
int n, ans;
char s1[N], s2[N], s12[2*N], s[2*N];
map<string, int>mymap;
int main()
{
int t;
int cas = 1;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
scanf("%s%s%s", s1, s2, s12);
int len, flag = 0;
ans = 0;
while(1)
{
ans++;
len = 0;
for(int i = 0; i < n; i++)//洗牌
{
s[len++] = s2[i];
s[len++] = s1[i];
}
s[len] = '\0';
if(strcmp(s, s12) == 0)
{
flag = 1; break;
}
if(mymap.find(s) != mymap.end()) break; //重复出现
mymap[s] = 0;
for(int i = 0; i < n; i++)//重新洗牌
{
s1[i] = s[i];
s2[i] = s[i + n];
}
}
if(flag) printf("%d %d\n", cas++, ans);
else printf("%d -1\n", cas++);
}
return 0;
}