Cod sursa(job #2298447)

Utilizator stefantagaTaga Stefan stefantaga Data 8 decembrie 2018 10:30:24
Problema Prefix Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <fstream>
#include <cstring>
using namespace std;
ifstream f("prefix.in");
ofstream g("prefix.out");
char s[1000005];
int t,i,m,pi[1000005],j,k;
int main()
{
    f>>t;f.get();
    for (i=1;i<=t;i++)
    {
        f.getline (s,1000005);
        k=0;
        m=strlen(s);
        pi[0]=0;
        for (j=1;j<m;j++)
        {
            k=pi[j-1];
            while (k>0&&s[j]!=s[k])
            {
                k=pi[k-1];
            }
            if (s[j]==s[k])
            {
                k++;
            }
            pi[j]=k;
        }
        int of=0;
        for (j=0;j<m;j++)
        {
            if (j+1-pi[j]<j+1)
            {
                if ((j+1)%(j+1-pi[j])==0)
            {
                of=j+1;
            }
            }
        }
        g<<of<<'\n';
    }
    return 0;
}