-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path1026.cc
64 lines (51 loc) · 1.28 KB
/
1026.cc
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
//Name: Cipher
//Level: 3
//Category: 行列,置換
//Note:
/*
* 置換が列方向から見た状態で与えられているため,行方向に直してから累乗する.
*/
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
vector<int> shuffle_pow(const vector<int> &tr, int n) {
if(n == 1) return tr;
const int N = tr.size();
vector<int> half = shuffle_pow(tr, n/2);
vector<int> res(tr.size());
for(int i = 0; i < N; ++i) res[i] = half[half[i]];
if(n % 2 == 1) {
half.swap(res);
for(int i = 0; i < N; ++i) res[i] = half[tr[i]];
}
return res;
}
int main() {
ios::sync_with_stdio();
while(true) {
int N;
cin >> N;
if(!N) break;
vector<int> tr(N);
for(int i = 0; i < N; ++i) {
int a;
cin >> a;
tr[a-1] = i;
}
while(true) {
int K;
string msg;
cin >> K;
if(!K) break;
cin.ignore();
getline(cin, msg);
vector<int> tr_pow = shuffle_pow(tr, K);
for(int i = 0; i < N; ++i) cout << (tr_pow[i]<msg.size() ? msg[tr_pow[i]] : ' ');
cout << endl;
}
cout << endl;
}
return 0;
}