Cod sursa(job #2661382)

Utilizator Gheorghita_VladGheorghita Vlad Gheorghita_Vlad Data 21 octombrie 2020 21:40:15
Problema Prefix Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.62 kb
#include <fstream>
#include <cstring>
using namespace std;
ifstream f("prefix.in");
ofstream g("prefix.out");
char s[1000005];
int lps[1000005],m;
int buildLPS(char st[])
{
    int i,k=0,n;
    n=strlen(st);
    lps[1]=0;
    for (i=2;i<=n;i++)
    {
        while (k>0 && st[k]!=st[i-1]) k=lps[k];
        if (st[i-1]==st[k]) k++;
        lps[i]=k;
    }
    for (i=n;i>=1;i--)
        if (lps[i]!=0 && i%(i-lps[i])==0) return i;
    return 0;
}
int main()
{
    int i;
    f>>m;
    f.get();
    for (i=1;i<=m;i++)
    {
        f.getline(s,1000005);
        g<<buildLPS(s)<<'\n';
    }
    return 0;
}