Link: poj 3087
There are two sets of cards S1, S2, and the number of cards are all c. S12 is formed by crossover of S2 and S1 according to the given rules,
Then, the bottom C block of S12 is given to S1, and the top C block is given to S2. Based on this cycle,
Enter the initial status of S1 and S2 and the expected final status S12,
Ask how many shuffles S1 S2 can reach S12. If it is never the same, it will output "-1 ".
Analysis: simply simulate this rule. The key is to determine whether the expected S12 is always impossible,
If S1 and S2 are in the status after shuffling, It is a status that has appeared before shuffling, and this status is not the expected status S12,
It means that the shuffles cannot reach S12 no matter how they are shuffled, because the shuffling operation is already in a "ring"
If the status has not been repeated, shuffles are simulated until S12 appears.
# Include <stdio. h> # include <string. h> int main () {int n, m, I, J, K, sum, flag; char S1 [105], S2 [105], T [100] [210], s [210]; scanf ("% d", & N); For (k = 1; k <= N; k ++) {scanf ("% d", & M); scanf ("% S % s", S1, S2, S); j = 0; flag = 1; for (sum = 1; sum ++) {for (I = 0; I <m; I ++) {// set S2, s1 synthesis S12 T [J] [I * 2] = S2 [I]; t [J] [I * 2 + 1] = S1 [I];} T [J] [M * 2] = 0; // remember to add the null character if (strcmp (T [J], S) = 0) // determine whether the break is the same as the expected break; for (I = 0; I <j; I ++) if (strcmp (T [I], t [J]) = 0 ){// Flag = 0; break;} If (! Flag) {sum =-1; break;} strncpy (S1, t [J], M); // divides S12 into S1, S2 strncpy (S2, T [J] + M, m); j ++;} printf ("% d \ n", K, sum);} return 0 ;}