1690F - Shifting String - CodeForces Solution


graphs math number theory strings *1700

Please click on ads to support us..

Python Code:

def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)

def rotations(t):
    for i in range(1, len(t)+1):
        if t == t[i:] + t[:i]:
            return i

for _ in range(int(input())):
    n = int(input())
    s = input()
    p = [int(i)-1 for i in input().split()]

    visited = [False]*n
    ans = 1
        for i in range(n):
        if visited[i]:
            continue
        visited[i] = True
        j = p[i]
        t = s[i]
        while j != i:
            t += s[j]
            visited[j] = True
            j = p[j]
        rotation = rotations(t)
        ans = ans*rotation//gcd(ans, rotation)
    print(ans)

C++ Code:

// Problem: F. Shifting String
// Contest: Codeforces - Codeforces Round #797 (Div. 3)
// URL: https://codeforces.com/problemset/problem/1690/F
// Memory Limit: 256 MB
// Time Limit: 3000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
#define pb push_back
#define endl '\n'
#define mod 1000000007
#define N 500005
#define vi vector<int>
#define vvi vector<vector<int>>
#define vb vector<bool>
#define control ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define int long long int
#define ffor(i, n) for(int i=0; i<n; i++)
#define fforr(i, n) for(int i=1; i<=n; i++)
#define rfor(i, n) for(int i=n-1; i>=0; i--)
#define rforr(i, n) for(int i=n; i>0; i--)
#define all(x) x.begin(), x.end()
#define F  first
#define S  second
#define inf 9223372036854775807
using namespace std;
void setIO()
{	
	control;
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
}
void print(bool chk){
	if(chk){
		cout << "YES" << endl;
	}else{
		cout << "NO" << endl;
	}
}
int min(int a, int b){
	return a>b?b:a;
}
int max(int a, int b){
	return a<b?b:a;
}
int power(int x,int y,int p)
{
    int ans=1;
    x=x%p;
    if(x==0)
    {
    	return 0;
    }
    while(y>0)
    {
        if(y&1)
        {
        	ans=(ans*x)%p;
        }
        y=y>>1;
        x=(x*x)%p;
    }
    return ans;
}
int32_t main(){
	int t; cin >> t;
	while(t--){
		int n; cin >> n;
		string s; cin >> s;
		vi v(n);
		ffor(i, n){
			cin >> v[i];
			v[i]--;
		}
		vi vis(n);
		vi res;
		ffor(i, n){
			if(!vis[i]){
				vi temp;
				temp.pb(i);
				vis[i] = 1;
				int x = v[i];
				while(x != i){
					vis[x] = 1;
					temp.pb(x);
					x = v[x];
				}	
				int d = temp.size();
				bool test = 0;
				int mn = INT_MAX;
				for(int j=1; j*j<=d; j++){
					if(d%j == 0){
						int l = j, r = d/j;
						bool flag = 1;
						for(int k=0; k<l; k++){
							for(int p=k+l; p<d; p+=l){
								if(s[temp[p]] != s[temp[k]]){
									flag = 0;
									break;
								}
							}
							if(!flag)break;
						}
						if(flag){
							mn = min(mn, l);
						}
						flag = 1;
						for(int k=0; k<r; k++){
							for(int p=k+r; p<d; p+=r){
								if(s[temp[p]] != s[temp[k]]){
									flag = 0;
									break;
								}
							}
							if(!flag)break;
						}
						if(flag){
							mn = min(mn, r);
						}
					}
					
				}
				res.pb(mn);
			}
		}
		int ans = 1;
		for(int x:res){
			ans = ans*x/__gcd(ans, x);
		}
		cout << ans << endl;
	}
}


Comments

Submit
0 Comments
More Questions

275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome