Cod sursa(job #2561485)

Utilizator MatteoalexandruMatteo Verzotti Matteoalexandru Data 28 februarie 2020 20:39:56
Problema Prefix Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.64 kb
/*
                `-/oo+/-   ``
              .oyhhhhhhyo.`od
             +hhhhyyoooos. h/
            +hhyso++oosy- /s
           .yoooossyyo:``-y`
            ..----.` ``.-/+:.`
                   `````..-::/.
                  `..```.-::///`
                 `-.....--::::/:
                `.......--::////:
               `...`....---:::://:
             `......``..--:::::///:`
            `---.......--:::::////+/`
            ----------::::::/::///++:
            ----:---:::::///////////:`
            .----::::::////////////:-`
            `----::::::::::/::::::::-
             `.-----:::::::::::::::-
               ...----:::::::::/:-`
                 `.---::/+osss+:`
                   ``.:://///-.
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cmath>

using namespace std;

const int INF = 2e9;

const int N = 1e6;

char v[5 + N];
int pi[5 + N];

int main() {
    freopen("prefix.in", "r", stdin);
    freopen("prefix.out", "w", stdout);
    int t, n;
    scanf("%d\n", &t);

    while(t--){
        int ans(0);
        fgets(v, 3 + N, stdin);
        n = strlen(v) - 1;

        for(int i = 1; i <= n; i++){
            int j;

            j = pi[i - 1];
            while(j > 0 && v[i] != v[j])
                j = pi[j - 1];

            if(v[i] == v[j]) j++;
            pi[i] = j;

            if(pi[i - 1] > 0 && i % (i - pi[i - 1]) == 0)
                ans = max(ans, i);
        }

        printf("%d\n", ans);
    }
    return 0;
}