Problem Name | Periodic Strings |
Judge | UVA |
Problem Link | https://onlinejudge.org/external/4/455.pdf |
Algorithms & DS | Prefix Function |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll>pi;
void preFunc(string s)
{
ll len=(ll)s.size();
pi.resize(len+5);
pi[0]=0;
for(ll i=1;i<len;i++){
ll j=pi[i-1];
while((j>0)&&(s[i]!=s[j]))j=pi[j-1];
if(s[i]==s[j])j++;
pi[i]=j;
}
}
int main()
{
ll t;
cin>>t;
ll ck=0;
while(t--){
string s;
cin>>s;
ll len=s.size();
if(ck==0)ck=1;
else cout<<"\n";
preFunc(s);
ll ans=len;
ll tmp=len-pi[len-1];
if(len%tmp==0)ans=tmp;
cout<<ans<<"\n";
}
return 0;
}
Code language: PHP (php)