http://acm.hust.edu.cn/vjudge/contest/121377#problem/G
根据题意模拟即可
#include<stdio.h> #include<string.h> #include<math.h> #include<queue> using namespace std; #define maxn 300 char a[maxn*maxn], b[maxn*maxn], s1[maxn], s2[maxn], s[maxn*maxn]; int solve(int n, int step) { int j=0; for(int i=0; i<n; i++) { a[j++] = s2[i]; a[j++] = s1[i]; } a[j]='\0'; if(!strcmp(a, s)) return step; if(step == 1) strcpy(b, a); if(step!=1 && !strcmp(b, a)) return -1; strncpy(s1, a, n); strncpy(s2, a+n, n); return solve(n, step+1); } int main() { int T, t=1, n; scanf("%d", &T); while(T --) { scanf("%d %s %s %s", &n, s1, s2, s); int ans = solve(n, 1); printf("%d %d\n",t++, ans); } return 0; }