Cod sursa(job #2827461)

Utilizator SochuDarabaneanu Liviu Eugen Sochu Data 5 ianuarie 2022 19:15:36
Problema Prefix Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.08 kb
#include <bits/stdc++.h>
//#pragma GCC optimize ("03")
#define FastIO ios_base::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)
#define FILES freopen("prefix.in" , "r" , stdin) , freopen("prefix.out" , "w" , stdout)
#define ll long long
#define ull unsigned long long
#define ld long double
#define eb emplace_back
#define pb push_back
#define qwerty1 first
#define qwerty2 second
#define qwerty3 -> first
#define qwerty4 -> second
#define umap unordered_map
#define uset unordered_set
#define pii pair < int , int >
#define pq priority_queue
#define dbg(x) cerr << #x << ": " << x << '\n'

namespace FastRead
{
    char __buff[5000];int __lg = 0 , __p = 0;
    char nc()
    {
        if(__lg == __p){__lg = fread(__buff , 1 , 5000 , stdin);__p = 0;if(!__lg) return EOF;}
        return __buff[__p++];
    }
    template<class T>void read(T&__x)
    {
        T __sgn = 1; char __c;while(!isdigit(__c = nc()))if(__c == '-')__sgn = -1;
        __x = __c - '0';while(isdigit(__c = nc()))__x = __x * 10 + __c - '0';__x *= __sgn;
    }
}

using namespace FastRead;
using namespace std;

const int N = 1e6 + 10;
const int M = 1e9 + 7;
const ld PI = acos(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n;
int pi[N];
string s;

void kmp()
{
    pi[0] = 0;

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

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

        j += s[i] == s[j];
        pi[i] = j;
    }
}

void Test()
{
    int i , j;

    cin >> s;
    n = s.size();

    kmp();

    int ans = 0;

    for(i = 0 ; i + 1 < s.size() ; i++)
    {
        int lg = i + 1;

        for(j = i + 1 ; j + lg - 1 < s.size() ; j += lg)
        {
            int last = j + lg - 1;

            if(pi[last] >= lg && last - pi[last] + 1 > i)
                ans = max(ans , last + 1);
            else break;
        }
    }

    cout << ans << '\n';
}

signed main()
{
	#ifndef ONLINE_JUDGE
		FastIO , FILES;
	#endif

	int q; cin >> q;
	while(q--) Test();

    return 0;
}