Cod sursa(job #3300008)

Utilizator rapidu36Victor Manz rapidu36 Data 12 iunie 2025 11:07:18
Problema Prefix Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int lung_max_per(string &a)
{
    int n = (int)a.length();
    vector <int> pi(n);
    int lung_pref = 0, lung_pref_per = 0;
    pi[0] = lung_pref;
    for (int i = 1; i < n; i++)
    {
        while (lung_pref > 0 && a[i] != a[lung_pref])
        {
            lung_pref = pi[lung_pref-1];
        }
        if (a[i] == a[lung_pref])
        {
            lung_pref += 1;
        }
        pi[i] = lung_pref;
        if (lung_pref != 0 && (i + 1) % (i + 1 - lung_pref) == 0)
        {
            lung_pref_per = i + 1;
        }
    }
    return lung_pref_per;
}

int main()
{
    ifstream in("prefix.in");
    ofstream out("prefix.out");
    int t;
    in >> t;
    for (int i = 0; i < t; i++)
    {
        string s;
        in >> s;
        out << lung_max_per(s) << "\n";
    }
    in.close();
    out.close();
    return 0;
}