Cod sursa(job #2951260)

Utilizator Dragono63Stanciu Rares Stefan Dragono63 Data 5 decembrie 2022 19:43:31
Problema Prefix Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.76 kb
#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int, int>
using ll = long long;

using namespace std;

const int NMAX = 2e6 + 5;
/*******************************/
// INPUT / OUTPUT

ifstream f("prefix.in");
ofstream g("prefix.out");
/*******************************/
/// GLOBAL DECLARATIONS

int T;
int N;
int ans;
int pi[NMAX];
string s;
/*******************************/
/// FUNCTIONS

void ReadInput();
void Solution();
/*******************************/
///-------------------------------------
inline void ReadInput()
{
    f >> T;
}
///-------------------------------------
inline void Reset()
{
    memset(pi, 0, sizeof(pi));
}
///-------------------------------------
inline void Read()
{
    f >> s;
    ans = 0;
    N = s.length();
}
///-------------------------------------
inline void Solve()
{
    int j;
    for (int i = 1 ; i < N ; ++ i)
    {
        j = pi[i - 1];
        while (j && s[j] != s[i])
            j = pi[j - 1];
        if (s[i] == s[j]) j ++;
        pi[i] = j; 
    }
    
    int cnt;
    for (int len = 1 ; 2 * len <= N ; ++ len)
    {
        cnt = 1;
        for (int i = 2 * len - 1 ; i < N ; i += len)
        {
            if (pi[i] == i - len + 1) cnt ++;
            else break;
        }
        if (cnt >= 2) ans = max(ans, cnt * len);
    }
}
///-------------------------------------
inline void Output()
{
    g << ans << "\n";
}
///-------------------------------------
inline void TestCase()
{
    Reset();
    Read();
    Solve();
    Output();
}
///-------------------------------------
inline void Solution()
{
    while (T --)
    {
        TestCase();
    }
}
///-------------------------------------
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ReadInput();
    Solution();
    return 0;
}