Submission #516789

# Submission time Handle Problem Language Result Execution time Memory
516789 2022-01-22T06:25:23 Z shrimb Palindromes (APIO14_palindrome) C++17
47 / 100
1000 ms 8040 KB
#include"bits/stdc++.h"
using namespace std;
#define int long long
#define endl '\n'
const int mod = 1e9 + 7;
const int smolp = 31;

string s;
int n;
int pref[300001];
int suff[300001];
int inv[300001];

int pw (int a, int b) {
    if (b == 0) return 1;
    if (b == 1) return a;
    if (b & 1) return (a * pw(a, b - 1)) % mod;
    int y = pw(a, b / 2);
    return (y*y)%mod;
}

int Solve (int sz) {
    map<int,int>f;
    int mxf = 0;
    for (int i = sz - 1 ; i < n ; i++) {
        int sub = pref[i] - (i - sz < 0 ? 0 : pref[i-sz]);
        if (sub < 0) sub += mod;
        sub *= inv[i - sz + 1];
        sub %= mod;

        int sub2 = suff[i-sz+1] - (i < n - 1 ? suff[i + 1] : 0);
        if (sub2 < 0) sub2 += mod;
        sub2 *= inv[(n-1) - i];
        sub2 %= mod;

        // cerr << sz << ": " << sub << " " << sub2 << endl;
        if (sub == sub2) mxf = max(mxf, ++f[sub]);
    }
    return mxf;
}

signed main() {
    ios::sync_with_stdio(false); cin.tie(0);
    cin >> s;
    n = s.size();

    int x = 1;
    for (int i = 0 ; i <= n ; i++) {
        inv[i] = pw(x, mod - 2);
        x *= smolp;x%=mod;
    }

    int pp = 1;

    for (int i = 0 ; i < n ; i++) {
        int prev_p = (i ? pref[i-1] : 0);
        pref[i] = prev_p + ((pp * (s[i]-'a'))%mod);
        pref[i] %= mod;
        pp *= smolp;
        pp %= mod;
    }

    pp = 1;

    for (int i = n - 1 ; i >= 0 ; i--) {
        int prev_s = (i < n - 1  ? suff[i + 1] : 0);
        suff[i] = prev_s + ((pp * (s[i] - 'a')) % mod);
        suff[i] %= mod;
        pp *= smolp;
        pp %= mod;
    }

    int mx_ans = 0;

    for (int i = 1 ; i <= n ; i++) {
        // cerr << i << " " << Solve(i) << endl;
        mx_ans = max(mx_ans, Solve(i) * i);
    }
    cout << mx_ans << endl;

}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 312 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 308 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 1 ms 308 KB Output is correct
18 Correct 0 ms 320 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 316 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 1 ms 300 KB Output is correct
24 Correct 1 ms 324 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 312 KB Output is correct
27 Correct 0 ms 316 KB Output is correct
28 Correct 1 ms 320 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 316 KB Output is correct
31 Correct 1 ms 324 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 324 KB Output is correct
2 Correct 3 ms 316 KB Output is correct
3 Correct 4 ms 332 KB Output is correct
4 Correct 4 ms 364 KB Output is correct
5 Correct 4 ms 332 KB Output is correct
6 Correct 4 ms 332 KB Output is correct
7 Correct 5 ms 332 KB Output is correct
8 Correct 4 ms 332 KB Output is correct
9 Correct 4 ms 316 KB Output is correct
10 Correct 3 ms 332 KB Output is correct
11 Correct 4 ms 320 KB Output is correct
12 Correct 3 ms 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 276 ms 588 KB Output is correct
2 Correct 295 ms 580 KB Output is correct
3 Correct 288 ms 580 KB Output is correct
4 Correct 313 ms 588 KB Output is correct
5 Correct 232 ms 572 KB Output is correct
6 Correct 233 ms 572 KB Output is correct
7 Correct 235 ms 572 KB Output is correct
8 Correct 223 ms 588 KB Output is correct
9 Correct 231 ms 588 KB Output is correct
10 Correct 221 ms 564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1053 ms 2964 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1068 ms 8040 KB Time limit exceeded
2 Halted 0 ms 0 KB -