제출 #107954

#제출 시각아이디문제언어결과실행 시간메모리
107954SamAndPalindromes (APIO14_palindrome)C++17
23 / 100
705 ms132096 KiB
#include <bits/stdc++.h>
using namespace std;
const int N = 10004;
const long long P = 31;

int n;
char aa[N], a[N];

long long ast[N];
long long p[N], s[N];

bool pal(int l, int r)
{
    return (p[r] - p[l - 1]) * ast[n - r] == (s[l] - s[r + 1]) * ast[l - 1];
}

int z;
map<int, int> t[N * 200];
int b[N * 200];

int main()
{
    cin >> aa;
    n = strlen(aa);
    for (int i = 1; i <= n; ++i)
        a[i] = aa[i - 1];

    ast[0] = 1;
    for (int i = 1; i <= n; ++i)
        ast[i] = ast[i - 1] * P;

    for (int i = 1; i <= n; ++i)
        p[i] = p[i - 1] + ast[i - 1] * (a[i] - 'a' + 1);
    for (int i = n; i >= 1; --i)
        s[i] = s[i + 1] + ast[n - i] * (a[i] - 'a' + 1);

    for (int l = 1; l <= n; ++l)
    {
        int pos = 0;
        for (int r = l; r <= n; ++r)
        {
            if (t[pos].find(a[r] - 'a') == t[pos].end())
                t[pos][a[r] - 'a'] = ++z;
            pos = t[pos][a[r] - 'a'];
            b[pos]++;
        }
    }

    int ans = 0;
    for (int l = 1; l <= n; ++l)
    {
        int pos = 0;
        for (int r = l; r <= n; ++r)
        {
            pos = t[pos][a[r] - 'a'];
            if (pal(l, r))
                ans = max(ans, b[pos] * (r - l + 1));
        }
    }

    cout << ans << endl;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...