http://poj.org/problem?id=3087
一道模拟题折腾了好久
View Code
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<map> 6 using namespace std; 7 map<string,int>ff; 8 int main() 9 { 10 int i,j,k,n; 11 char s1[110],s2[110],s[300],ss[300]; 12 cin>>n; 13 for(i = 1; i <= n ; i++) 14 { 15 cin>>k; 16 getchar(); 17 gets(s1); 18 gets(s2); 19 gets(s); 20 int k1 = 0,k2=0,num=0; 21 cout<<i<<" "; 22 while(1) 23 { 24 k1 = 0; 25 k2 = 0; 26 num++; 27 for(j = 0 ; j < 2*k ;j++) 28 { 29 if(j%2==0) 30 ss[j] = s2[k1++]; 31 else 32 ss[j] = s1[k2++]; 33 } 34 ss[2*k] = '\0'; 35 if(strcmp(ss,s)==0) 36 { 37 cout<<num<<endl; 38 break; 39 } 40 if(ff[ss]) 41 { 42 cout<<"-1"<<endl; 43 break; 44 } 45 ff[ss] = 1; 46 for(j = 0 ; j < k ; j++) 47 s1[j] = ss[j]; 48 for(j = k ; j < 2*k ; j++) 49 s2[j-k] = ss[j]; 50 } 51 } 52 return 0; 53 }