Cod sursa(job #875909)

Utilizator stefanzzzStefan Popa stefanzzz Data 10 februarie 2013 22:06:50
Problema Prefix Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.63 kb
#include <fstream>
#define MAXC 1000007
using namespace std;
ifstream f("prefix.in");
ofstream g("prefix.out");

int t,x,pd[MAXC],mx;
char s[MAXC];

int main()
{
    int i,j;
    f>>t;
    f.getline(s+1,MAXC,'\n');
    for(i=1;i<=t;i++){
        f.getline(s+1,MAXC,'\n');
        mx=0;
        for(j=2;s[j]!='\0';j++){
            pd[j]=0;
            x=pd[j-1];
            while(x&&s[x+1]!=s[j])
                x=pd[x];
            if(s[x+1]==s[j])
                pd[j]=x+1;
            if(pd[j]&&(j%(j-pd[j])==0))
                mx=j;}
        g<<mx<<'\n';}
    f.close();
    g.close();
    return 0;
}