POJ 3087 Shuffle ' m up linear congruence, violence difficulty: 2

Source: Internet
Author: User
Tags strcmp

http://poj.org/problem?id=3087

Set: S1={a1,a2,a3,... AC}

S2={ac+1,ac+2,ac+3,.... A2C}

The

Together to become

Ac+1,a1,ac+2,a2 ... A2c,ac

After one conversion, it becomes

S1={ac+1,a1,ac+2 ...}

s2={... A2C,AC}

Corresponds to the ordinal of each number that occurs before the change is

+1,+2,+3....-c,-c+1,.....

To think of the whole chain as a ring is also equivalent to:

+1,+2,+3....+c,+c+1,.......

A1, for example, must return to A1 after the a1->a2->a4->a7....c times.

So the whole string passes a certain number of transformations will be back to the original state, just to determine whether to return to the original state before the target State can be

#include <cstdio>//a= (a+c+1)% (2*c) #include <cstring>using namespace Std;const int Maxn=1002;int C;char s1[ Maxn],s2[maxn],aim[maxn],org[maxn],tmp[maxn];void Shuffle () {for (int i=0;i<c;i++) {Tmp[2*i]=s2[i]                ;        Tmp[2*i+1]=s1[i]; } tmp[2*c]=0;}        int main () {int T;        scanf ("%d", &t);                for (int ti=1;ti<=t;ti++) {scanf ("%d%s%s%s", &c,s1,s2,aim);                Shuffle ();                strcpy (ORG,TMP);                int Ans=1;                        if (strcmp (Tmp,aim) ==0) {printf ("%d 1\n", TI);                Continue                } bool Fl=false; while (strcmp (org,tmp)!=0| |                        Ans==1) {strncpy (s1,tmp,c);                        strncpy (S2,TMP+C,C);                        Shuffle ();                        ans++;                        if (strcmp (Tmp,aim) ==0) {fl=true;        printf ("%d%d\n", Ti,ans);                        Break        }} if (!FL) printf ("%d-1\n", TI); } return 0;}

  

POJ 3087 Shuffle ' m up linear congruence, violence difficulty: 2

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.