Cod sursa(job #2846534)

Utilizator tibinyteCozma Tiberiu-Stefan tibinyte Data 9 februarie 2022 12:13:09
Problema Prefix Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("strmatch.in");
ofstream fout("strmatch.out");
int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int q;
    cin >> q;
    while (q--) {
        string s;
        cin >> s;
        s = '$' + s;
        int n = s.size() - 1;
        vector<int> pi(n + 1);
        for (int i = 2; i <= n; ++i) {
            int j = pi[i - 1];
            while (j && s[j + 1] != s[i]) {
                j = pi[j];
            }
            if (j == 0) {
                pi[i] = (s[1] == s[i]);
            }
            else {
                pi[i] = j + 1;
            }
        }
        int ans = 0;
        for (int i = 1; i <= n; ++i) {
            if (pi[i] && i % (i - pi[i]) == 0) {
                ans = i;
            }
        }
        cout << ans << '\n';
    }

}