简单模拟,每次看当前的牌是否与初始状态相同,若相同则说明进入循环。
View Code
#include#include #include #include using namespace std; #define maxl 205 char s1[maxl], s2[maxl], aim[maxl], st[maxl], s3[maxl]; int l; int main() { //freopen("t.txt", "r", stdin); int t; scanf("%d", &t); for (int ca = 0; ca < t; ca++) { scanf("%d", &l); scanf("%s", s1); scanf("%s", s2); scanf("%s", aim); strcpy(st, s1); strcpy(st + l, s2); int ans = 0; do { for (int i = 0; i < l; i++) { s3[i * 2 + 1] = s1[i]; s3[i * 2] = s2[i]; } s3[l * 2] = '\0'; ans++; if (strcmp(s3, st) == 0) { ans = -1; break; } for (int i = 0; i < l; i++) s1[i] = s3[i]; for (int i = 0; i < l; i++) s2[i] = s3[i + l]; }while (strcmp(s3, aim) != 0); printf("%d %d\n", ca + 1, ans); } return 0; }