模拟就好了。。。只不过洗牌的阶段模拟容易出错,而且我用数组来模拟,代码不好看。归根结底还是对map和str函数操作不熟悉。
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int main(void){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
int num;
scanf("%d",&num);
int s1[1000+5]={0},s2[1000+5]={0};
int temp1[2][1000+5],temp2[2][1000+5];
for(int j=0;j<num;j++){
char c;
cin>>c;
s1[j]=c-'A' ;
temp1[0][j]=c-'A';
}
for(int j=0;j<num;j++){
char c;
cin>>c;
s2[j]=c-'A';
temp2[0][j]=c-'A';
}
int ans[2000+5]={0};
for(int j=0;j<2*num;j++){
char c;
cin>>c;
ans[j]=c-'A';
}
int j=1,ct=0;
printf("%d ",i);
while(1){
ct++;
for(int k=0;k<num;k++){
if(!(num%2)){
if(k%2){
temp1[j][k]=temp1[(j+1)%2][k/2];
temp2[j][k]=temp1[(j+1)%2][k/2+num/2];
}
else{
temp1[j][k]=temp2[(j+1)%2][k/2];
temp2[j][k]=temp2[(j+1)%2][k/2+num/2];
}
}
else{
if(k%2){
temp1[j][k]=temp1[(j+1)%2][k/2];
temp2[j][k]=temp2[(j+1)%2][(k+1)/2+num/2];
}
else{
temp1[j][k]=temp2[(j+1)%2][k/2];
temp2[j][k]=temp1[(j+1)%2][(k+1)/2+num/2];
}
}
}
int yes=1,yes2=1;
for(int k=0;k<num;k++){
if(temp1[j][k]!=ans[k]||temp2[j][k]!=ans[k+num]){
yes=0;
}
if(temp1[j][k]!=s1[k]||temp2[j][k]!=s2[k]){
yes2=0;
}
}
if(yes){
printf("%d\n",ct);
break;
}
else if(yes2){
printf("-1\n");
break;
}
else{
j=(j+1)%2;
}
}
}
}