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