Cod sursa(job #3323072)

Utilizator Radu_BicliBiclineru Radu Radu_Bicli Data 16 noiembrie 2025 21:20:23
Problema Prefix Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("prefix.in");
ofstream fout("prefix.out");
int n, i, j, lpf[1000002], k, lg, nr;
string s;

static inline void Test() {
    fin >> s;
    n = s.size();

    j = 0;
    for(i = 1; i < n; i++) {
        while(0 < j && s[i] != s[j]) j = lpf[j - 1];
        if(s[i] == s[j]) j++;
        lpf[i] = j;
    }

    j = n - 1;

    while(0 < j) {
        if(0 != lpf[j]) {
            lg = j - lpf[j];
            nr = 0;
            k = j;

            while(k - lpf[k] == lg && 0 != lpf[k]) {
                k -= lg + 1;
                nr++;
            }
            if(k == lg) {
                fout << (nr + 1) * (lg + 1) << "\n";
                return;
            }
        }
        j--;
    }
    fout << "0\n";
}

int main() {
    //ios_base::sync_with_stdio(false);
    fin.tie(nullptr);
    fout.tie(nullptr);

    int t;
    fin >> t;
    while(t--)
        Test();

    return 0;
}