Submission #955473

#TimeUsernameProblemLanguageResultExecution timeMemory
955473hoanganh_siuuuuuuuuPalindromes (APIO14_palindrome)C++17
47 / 100
1059 ms6672 KiB
#include <bits/stdc++.h>
#define int long long
#define f(i, a, b) for(int i = a; i <= b; i++)
#define fr(i, a, b) for(int i = a; i >= b; i--)
#define pii pair <int, int>
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define in insert
#define vvec vector<vector<int>>
#define fast ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0)

using namespace std;

const int N = 3e5 + 5;
const int mod = 1e9 + 7;
const int base = 31;
int n, Hash[N], Pow[N];
unordered_map <int, int> mp;
string s;

int get(int i, int j)
{
    return (Hash[j] - Hash[i - 1] * Pow[j - i + 1] + mod * mod) % mod;
}

void sub2()
{
    int res = 0;
    f(i, 1, n)
    {
        int t = 0;
        while(i - t >= 1 && i + t <= n && s[i - t] == s[i + t])
        {
            int z = ++mp[get(i - t, i + t)];
            // cout << get(i - t, i + t) << "\n";
            //cout << i << " " << z << " " << (2 * t + 1) << "\n";
            res = max(res, (2 * t + 1) * z);
            t++;
        }

        t = 0; int j = i + 1;
        while(i - t >= 1 && j + t <= n && s[i - t] == s[j + t])
        {
            int z = ++mp[get(i - t, j + t)];
            //cout << get(i - t, j + t) << "\n";
            //cout << i << " " << z << " " << (2 * t + 2) << "\n";
            res = max(res, (2 * t + 2) * z);
            t++;
        }
    }
    cout << res;
}

signed main()
{
    fast;
    if(fopen("palindrome.in", "r"))
    {
        freopen("palindrome.in", "r", stdin);
        freopen("palindrome.out", "w", stdout);
    }
    cin >> s;
    n = s.size(); s = '!' + s;
    Pow[0] = 1;
    f(i, 1, n)
    {
        //a[i] = s[i] - 'a';
        Hash[i] = (Hash[i - 1] * base + (s[i] - 'a' + 1)) % mod;
        Pow[i] = (Pow[i - 1] * base) % mod;
    }
    sub2();
    //int l = 0,
}

Compilation message (stderr)

palindrome.cpp: In function 'int main()':
palindrome.cpp:61:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |         freopen("palindrome.in", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
palindrome.cpp:62:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |         freopen("palindrome.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...