Cod sursa(job #2460586)

Utilizator FrostfireMagirescu Tudor Frostfire Data 23 septembrie 2019 22:55:17
Problema Prefix Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1 kb
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#define NMAX 10000000

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;
                }
            //for(int i=0; i<strlen(s); i++) cout << phi[i] << ' ';
            //cout << '\n';

            int sol = 0;
            for(int i=n-1; i>=0; i--)
                {   if(phi[i] && phi[i] % (i + 1 - phi[i]) == 0)
                    {   sol = i + 1;
                        break;
                    }
                }
            g << sol << '\n';
        }
    return 0;
}