因为交换的次数过多,所以必须找到循环节,进行取余,从而方便计算。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<list>
#include<queue>
#define mm(a,b) memset(a,b,sizeof(a))
#define ACCELERATE (ios::sync_with_stdio(false),cin.tie(0))
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define MAXN 0x3f3f3f3f3f3f3f3f
#define PI acos(-1.0)
#define E exp(1.0)
using namespace std;
//#define debug
int main()
{
#ifdef debug
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif // debug
int n;
int dec[205],cir[205];
bool flag[205];
while(scanf("%d",&n)!=EOF&&n){
for(int i=1;i<=n;i++){
scanf("%d",&dec[i]);
}
mm(flag,0);
for(int i=1;i<=n;i++){
if(!flag[i]){
int q=0,j=i;
while(!flag[j]){
q++;
flag[j]=1;
j=dec[j];
}
cir[i]=q;
for(j=dec[i];j!=i;j=dec[j]){
cir[j]=q;
}
}
}
char s[205],en[205];
mm(s,' ');
int k;
while(scanf("%d",&k)&&k){
getchar();
char c;
int len=1;
while(scanf("%c",&c)&&c!='\n'){
s[len++]=c;
}
mm(en,' ');
for(int i=1;i<len;i++){
int j=k%cir[i];
int po=i;
while(j--){
po=dec[po];
}
en[po]=s[i];
}
en[n+1]='\0';
printf("%s\n",en+1);
}
puts("");
}
return 0;
}