Cod sursa(job #3349889)

Utilizator eric_dragosDragos Eric eric_dragos Data 2 aprilie 2026 22:50:11
Problema Prefix Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ifstream fin("prefix.in");
ofstream fout("prefix.out");
const int Nmax = 1e6+5;
int lps[Nmax];
void test(string &s){
    int n = s.size();
    for(int i = 0; i<n; i++) lps[i] = 0;
    int i = 1, j = 0;
    while(i < n){
        if(s[i] == s[j]){
            lps[i++] = ++j; 
        }
        else if(j == 0){
            i++;
        }
        else{
            j = lps[j-1];
        }
    }
    for(int i = n; i>= 0; i--){
        int perioada = i-lps[i-1];
        if(i % perioada == 0 && lps[i-1] > 0){
            fout << i << '\n';
            break;
        }
    }
}
int main(){
    int t;
    fin >> t;
    while(t--){
        string s;
        fin >> s;
        test(s);
    }


    return 0;
}