-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1690F_Shifting-String.cpp
55 lines (51 loc) · 1.03 KB
/
1690F_Shifting-String.cpp
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
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int gcd(int a, int b){
if(!a) return b;
return gcd(b%a,a);
}
int lcm(int a, int b){
if(!a) return b;if(!b) return a;
return a*b/gcd(a,b);
}
signed main() {
IO
int t;
cin >> t;
while(t--){
int n;
cin >> n;
string s;
cin >> s;
vector<int> p(n);
for(int &it : p) {cin >> it; it--;}
int ans = 0;
bitset<200> visited = 0;
for(int i = 0;i<n;i++){
string t; t = s;
if(visited[i]) continue;
int pos = i, cnt = 1;
vector<int> v;
while(!visited[pos]){
v.push_back(pos);
t[pos] = s[p[pos]];
visited[pos] = 1;
pos = p[pos];
}
while(t!=s){
cnt++;
string c = t;
int jn = v.size();
for(int j = 0;j<jn;j++){
c[v[j]] = t[v[(j+1)%jn]];
}
t.clear();t = c;
}
ans = lcm(ans, cnt);
}
cout << ans << '\n';
}
return 0;
}