Cod sursa(job #3273202)

Utilizator nopreanOprean Natasha noprean Data 1 februarie 2025 11:28:05
Problema Prefix Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <fstream>
#include <vector>
using namespace std;

ifstream fin("prefix.in");
ofstream fout("prefix.out");

void make_prefix(string &s, vector<int> &p)
{
    int n = s.size();
    p.resize(n);
    for (int i = 1; i < n; ++i)
    {
        int j = p[i - 1];

        while (j && s[i] != s[j])
            j = p[j - 1];

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

        p[i] = j;
    }
}

void solve(string &s)
{
    vector<int> p;
    make_prefix(s, p);
    int max_res = 0;
    for (int i = 0; i < s.size(); ++i)
        if (p[i] && (i + 1) % (i + 1 - p[i]) == 0)
            max_res = max(max_res, i + 1);
    fout << max_res << '\n';
}

int main()
{
    int t;
    fin >> t;
    string s;
    while (t--)
    {
        fin >> s;
        solve(s);
    }
    return 0;
}