Cod sursa(job #3160084)

Utilizator not_anduAndu Scheusan not_andu Data 22 octombrie 2023 21:19:55
Problema Prefix Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.44 kb
/**
 * Author: Andu Scheusan (not_andu)
 * Created: 22.10.2023 20:58:15
*/

#include <bits/stdc++.h>
#pragma GCC optimize("O3")

using namespace std;

#define INFILE "prefix.in"
#define OUTFILE "prefix.out"

#define all(x) (x).begin(), (x).end()
#define MP make_pair
#define F first
#define S second

typedef long long ll;

void solve(){

    string s;

    cin >> s;

    int n = s.length();

    vector<int> pi(n);

    for(int i = 1; i < n; ++i){
        
        int j = pi[i - 1];

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

        if(s[i] == s[j]){
            ++j;
        }

        pi[i] = j;

    }

    bool ok = false;
    int i = n - 1;

    while(!ok && i > 0){

        if(pi[i] != 0){

            int position = i, diff = i - pi[i] + 1, aux = 0;

            while(position - pi[position] + 1 == diff && pi[position] != 0){
                position = position - diff;
                ++aux;
            }
            
            if(position + 1 == diff){
                ++aux;
                cout << aux * diff << '\n';
                ok = true;
            }

        }

        --i;

    }

    if(!ok){
        cout << 0 << '\n';
    }

}

int main(){
    
    ios_base::sync_with_stdio(false);

    freopen(INFILE, "r", stdin);
    freopen(OUTFILE, "w", stdout);

    cin.tie(nullptr);
    cout.tie(nullptr);

    int tests; cin >> tests;

    for(int i = 0; i < tests; ++i){
        solve();
    }

    return 0;
}