模拟题
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; #define maxn 205 int n, m, len; int f[maxn]; char st[maxn]; bool vis[maxn]; int circle_num; int circle[maxn]; int circle_length[maxn]; char ans[maxn]; void make() { memset(vis, 0, sizeof(vis)); circle_num = 0; for (int i = 0; i < n; i++) if (!vis[i]) { int num = 0, temp = i; while (!vis[temp]) { vis[temp] = true; circle[temp] = circle_num; num++; temp = f[temp]; } circle_length[circle_num++] = num; // printf("%d\n", num); } } void input() { for (int i = 0; i < n; i++) { scanf("%d", &f[i]); f[i]--; } make(); while (scanf("%d", &m), m) { getchar(); len = 0; gets(st); len = strlen(st); for (int i = len; i < n; i++) st[i] = ' '; st[n] = '\0'; for (int i = 0; i < n; i++) { int j = m % circle_length[circle[i]]; int temp = i; while (j--) temp = f[temp]; ans[temp] = st[i]; } ans[n] = '\0'; printf("%s\n", ans); } } int main() { //freopen("t.txt", "r", stdin); while (scanf("%d", &n), n) { input(); putchar('\n'); } return 0; }