1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
#include <iostream> #include <map> #include <vector> #include <string> #include <stdio.h> /* 10 4 5 3 7 2 8 1 6 10 9 1 Hello Bob 1995 CERC 0 0 Sample Output BolHeol b C RCE */ using namespace std; int arr[202]; vector<char> c_arr[202]; char s[202]; int cycle(int pos) { int org=pos; int init=arr[pos]; int cnt=1; c_arr[pos-1].clear(); c_arr[org-1].push_back(s[arr[pos]]); pos=arr[pos]; while(init!=arr[pos]) { c_arr[org-1].push_back(s[arr[pos]]); pos=arr[pos]; cnt++; } return cnt; } int main() { int n,k,len; string str; int pos_of_cycle; while(cin>>n) { if(n==0) break; for(int i=1;i<=n;i++) { cin>>arr[i]; } while(cin>>k) { getchar(); if(k==0) break; getline(cin,str); for(int i=0;i<str.size();i++) { s[i+1]=str[i]; } for(int i=(str.size()+1);i<=n;i++) s[i]=' '; for(int pos=1;pos<=n;pos++) { len = cycle(pos); pos_of_cycle=len-1-(k%len); cout<<c_arr[pos-1][pos_of_cycle]; } cout<<"\n"; } cout<<"\n"; } return 0; } |