Cod sursa(job #2717298)

Utilizator beingsebiPopa Sebastian beingsebi Data 7 martie 2021 00:10:02
Problema Prefix Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("prefix.in");
ofstream g("prefix.out");
// #define f cin
// #define g cout
void solve()
{
    static int pref[1000001];
    static string a;
    string s("#");
    static int n;
    int maxim = 0;
    f >> a;
    n = a.size();
    s += a;
    pref[1] = 0;
    for (int i = 2; i <= n; i++)
    {
        pref[i] = pref[i - 1];
        while (pref[i] && s[i] != s[pref[i] + 1])
            pref[i] = pref[pref[i]];
        if (s[i] == s[pref[i] + 1])
            pref[i]++;
        int tot = i, poz = pref[i], ok = 1;
        while (true)
        {
            if (2 * poz < tot)
            {
                ok = 0;
                break;
            }
            if (tot % poz == 0)
                break;
            tot = poz;
            poz = pref[poz];
        }
        if (ok)
            maxim = max(maxim, i);
    }
    g << maxim << '\n';
}
int main()
{
    int q;
    f >> q;
    while (q--)
        solve();
    return 0;
}