Cod sursa(job #2462359)
Utilizator | Cojocaru Andrei Cristian Cojocaru_Andrei_Cristian | Data | 27 septembrie 2019 10:22:37 |
---|---|---|---|
Problema | Prefix | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva de probleme | Marime | 0.85 kb |
#include <bits/stdc++.h>
using namespace std;
int v[1000005];
char s[1000005];
int main()
{
ifstream cin("prefix.in");
ofstream cout("prefix.out");
int t;
cin>>t;
for(int j=1;j<=t;j++)
{
cin>>s;
int n=strlen(s);
v[0]=-1;
for(int i=1; i<n; i++)
{
long long aux=i-1;
while(s[v[aux]+1]!=s[i]&&v[aux]!=-1)
aux=v[aux];
if (s[v[aux]+1]==s[i])
v[i]=v[aux]+1;
else
v[i]=-1;
}
int ans=0;
for(int i=n-1; i>=0; i--)
if(v[i]!=-1)
if((i+1)%(i-v[i])==0)
{
ans=i+1;
break;
}
cout<<ans<<endl;
memset(v,0,sizeof(v));
}
return 0;
}