Poj 3087 shuffle'm up (simulation)

Source: Internet
Author: User
Tags shuffle strcmp

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 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.