1 /**
2 思路:简单置换群,每一个数均有一个置换循环,求出循环大小
3 对于每一组循环,真正循环次数=总置换次数%循环数
4 **/
5 #include<stdio.h>
6 #include<string.h>
7
8 int calc(int i,int n,int num[]){//check:ok
9 int count=1,a,j;
10 a=num[i-1];
11 j=num[a-1];
12 while(i!=a){
13 count++;
14 i=num[j-1];
15 j=i;
16 }
17 return count;
18 }
19
20 int main(){
21 int n,k,num[200],final_pos[200],time[200];
22 char msg[201],final_msg[201];
23 lab:while(scanf("%d",&n)){
24 if(n==0)break;
25 for(int i=0;i<n;i++){
26 scanf("%d",&num[i]);
27 }
28 for(int i=0;i<n;i++)time[i]=calc(i+1,n,num);
29 while(scanf("%d",&k)){
30 if(k==0){
31 printf("\n");
32 goto lab;
33 }
34 char space;
35 scanf("%c",&space);
36 int flag=0;
37 for(int i=0;i<n;i++){
38 if(flag==0)scanf("%c",&msg[i]);
39 if(msg[i]=='\n'||msg[i]=='\r'){
40 flag=1;
41 msg[i]='';
42 }else if(flag==1)msg[i]='';
43 }
44 msg[n]='\0';
45 for(int i=0;i<n;i++){
46 int j=i,real_count;
47 real_count=k%time[i];
48 if(real_count==0)final_pos[i]=i+1;
49 else{
50 for(int m=0;m<real_count;m++){
51 final_pos[i]=num[j];
52 j=num[j]-1;
53 }
54 } }
55 for(int i=1;i<=n;i++){
56 for(int j=0;j<n;j++){
57 if(final_pos[j]==i)final_msg[i-1]=msg[j];
58 }
59 }
60 final_msg[n]='\0';
61 puts(final_msg);
62 }
63 }
64 return 0;
65 }
-------------------------------~问世间情为何物,敲敲代码停不住~
-------------------------------