Cod sursa(job #2460589)

Utilizator FrostfireMagirescu Tudor Frostfire Data 23 septembrie 2019 22:59:31
Problema Prefix Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <fstream>
#include <cstring>
#include <string>
#define NMAX 1000000

using namespace std;

ifstream f("prefix.in");
ofstream g("prefix.out");

int t, phi[NMAX+10];
string s;

int main()
{
    f >> t;
    while(t--)
        {   f >> s;
            memset(phi, 0, sizeof(phi));
            int n = s.size();
            for(int i=1; i<n; i++)
                {   int x = i - 1;
                    while(s[phi[x]] != s[i] && phi[x]) x = phi[x]-1;
                    if(s[phi[x]] == s[i]) phi[i] = phi[x] + 1;
                    else phi[i] = 0;
                }
            int sol = 0;
            for(int i=n-1; i>=0; i--)
                {   if(phi[i] && (i+1) % (i+1-phi[i]) == 0)
                    {   sol = i + 1;
                        break;
                    }
                }
            g << sol << '\n';
        }
    return 0;
}