置换群就可以搞定!!!
注意下格式就好了……
1 #include<iostream> 2 #include<stdio.h> 3 #include<algorithm> 4 #include<iomanip> 5 #include<cmath> 6 #include<stdlib.h> 7 #include<cstring> 8 #include<vector> 9 #define ll __int64 10 #define pi acos(-1.0) 11 #define MAX 50000 12 using namespace std; 13 int code[202],an[202]; 14 char str1[202],str2[202]; 15 void init(int n) 16 { 17 int i,j,t; 18 for (i=1;i<=n;i++){ 19 j = i; 20 t = 0; 21 while (code[j]!=i) j = code[j],t++; 22 an[i] = t+1; 23 } 24 } 25 int main(){ 26 int n,m,i,j,k,t; 27 while (cin>>n&&n){ 28 for (i=1;i<=n;i++) 29 cin>>code[i]; 30 init(n); 31 while (scanf("%d",&k)!=EOF&&k){ 32 getchar(); 33 gets(str1+1); 34 for (i=strlen(str1+1)+1;i<=n;i++) 35 str1[i] =' '; 36 for (i=1;i<=n;i++){ 37 t = k%an[i]; 38 j = i; 39 if (t==0) str2[i] = str1[i]; 40 while (t--){ 41 j = code[j]; 42 } 43 str2[j] = str1[i]; 44 } 45 for (i=1;i<=n;i++) 46 cout<<str2[i]; 47 cout<<endl; 48 } 49 cout<<endl; 50 } 51 return 0; 52 }