Pagini recente » Cod sursa (job #2218498) | Cod sursa (job #2203785) | Cod sursa (job #1166370) | Cod sursa (job #2581672) | Cod sursa (job #1164221)
#include<stdio.h>
#include<string.h>
const int N=1000001;
using namespace std;
FILE *f,*g;
int t; int kmp[N]; char c[N];
void read(void){
f=fopen("prefix.in","r");
g=fopen("prefix.out","w");
fscanf(f,"%d",&t);
}
void knuthmp(char c[],int n){
int i; int k=0; int max=0;
kmp[1]=0;
for(i=2; i<=n; i++){
while (k>0 && c[k+1] != c[i]) k=kmp[k];
if (c[k+1] == c[i]) ++k;
kmp[i]=k;
if (kmp[i] != 0 && i % (i-kmp[i]) == 0) max=i;
}
fprintf(g,"%d\n",max);
}
void solve(void){
int i;int j;int n;
for(i=1;i<=t;i++){
fscanf(f,"%s\n",c);
n=strlen(c);
for(j=n;j>=1;j--) c[j]=c[j-1];
knuthmp(c,n);
}
}
int main(void){
read();
solve();
return 0;
}