답안 #249177

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
249177 2020-07-14T13:06:00 Z receed 회문 (APIO14_palindrome) C++14
100 / 100
381 ms 17292 KB
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cassert>
#include <string>
#include <set>
#include <map>
#include <random>
#include <bitset>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <queue>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;
using ul = unsigned long long;
using ld = long double;

const int N = 600001, M = 1 << 19;
string s;
int mp[N], mx[N], ord[N], c[N], cnt[N], a[N], n, pc[N], pord[N], lcp[N], pos[N], tr[M * 2];

int getl(int p, int m, int v=1, int l=0, int r=M) {
    if (tr[v] >= m || p <= l)
        return -1;
    if (r - l == 1)
        return l;
    int x = getl(p, m, v * 2 + 1, (l + r) / 2, r);
    if (x != -1)
        return x;
    return getl(p, m, v * 2, l, (l + r) / 2);
}

int getr(int p, int m, int v=1, int l=0, int r=M) {
    if (tr[v] >= m || r <= p)
        return n;
    if (r - l == 1)
        return l;
    int x = getr(p, m, v * 2, l, (l + r) / 2);
    if (x != n)
        return x;
    return getr(p, m, v * 2 + 1, (l + r) / 2, r);
}

int sm(int x, int y) {
    return x + y >= n ? x + y - n : x + y;
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> s;
    n = s.size();
    int l = 0, r = 0;
    rep(i, 2 * n - 1) {
        if (i <= 2 * r)
            mp[i] = min(mp[(l + r) * 2 - i], r * 2 - i + 1);
        if (i % 2 == 0)
            mp[i] = max(mp[i], 1);
        while (i - mp[i] - 1 >= 0 && (i + mp[i] + 1) / 2 < n && s[(i - mp[i] - 1) / 2] == s[(i + mp[i] + 1) / 2])
            mp[i] += 2;
        if ((i + mp[i] - 1) / 2 > r) {
            l = (i - mp[i] + 1) / 2;
            r = (i + mp[i] - 1) / 2;
        }
    }
    int ps = 2 * n - 1;
    for (int i = n - 1; i >= 0; i--) {
        while (ps >= 0 && mp[ps] < ps - i * 2 + 1)
            ps--;        
        mx[i] = ps - i * 2 + 1;
    }
    // rep(i, 2 * n - 1)
    //     cout << mp[i] << '\n';
    // rep(i, n)
    //     cout << mx[i] << '\n';
    n++;
    rep(i, n) {
        a[i] = (i == n - 1 ? 0 : s[i] - 'a' + 1);
        cnt[a[i] + 1]++;
    }
    rep(i, 27)
        cnt[i + 1] += cnt[i];
    rep(i, n)
        ord[cnt[a[i]]++] = i;
    int cc = 1;
    rep(i, n - 1) {
        c[ord[i + 1]] = c[ord[i]];
        if (s[ord[i]] != s[ord[i + 1]]) {
            c[ord[i + 1]]++;
            cc++;
        }
    }
    for (int l = 1; l < n; l *= 2) {
        rep(i, cc + 1)
            cnt[i] = 0;
        rep(i, n) {
            cnt[c[i] + 1]++;
            pord[i] = ord[i];
            pc[i] = c[i];
        }
        rep(i, cc)
            cnt[i + 1] += cnt[i];
        rep(i, n) {
            int q = pord[i] - l;
            if (q < 0)
                q += n;
            ord[cnt[c[q]]++] = q;
        }
        c[ord[0]] = 0;
        rep(i, n - 1) {
            c[ord[i + 1]] = c[ord[i]];
            if (pc[ord[i]] != pc[ord[i + 1]] || pc[sm(ord[i], l)] != pc[sm(ord[i + 1], l)])
                c[ord[i + 1]]++;
        }
        cc = c[ord[n - 1]] + 1;
    }
    rep(i, n)
        pos[ord[i]] = i;
    cc = 0;
    rep(i, n) {
        cc = max(cc - 1, 0);
        // cc = 0;
        if (pos[i] == n - 1)
            cc = 0;
        else
            while (s[i + cc] == s[ord[pos[i] + 1] + cc])
                cc++;
        lcp[pos[i]] = cc;
    }
    // rep(i, n - 1)
    //     assert(s.substr(ord[i], lcp[i]) == s.substr(ord[i + 1], lcp[i]));
    // rep(i, n)
    //     cout << lcp[pos[i]] << '\n';
    rep(i, n) {
        // cout << ord[i] << '\n';
        // cout << s.substr(ord[i]) << '\n';
    }
    rep(i, n - 1)
        tr[M + i] = lcp[i];
    for (int i = M - 1; i > 0; i--)
        tr[i] = min(tr[i * 2], tr[i * 2 + 1]);
    ll ans = 0;
    rep(i, n) {
        ll cl = getl(pos[i], mx[i]), cr = getr(pos[i], mx[i]);
        ans = max(ans, (cr - cl) * mx[i]);
    }
    cout << ans;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2432 KB Output is correct
2 Correct 2 ms 2432 KB Output is correct
3 Correct 2 ms 2432 KB Output is correct
4 Correct 2 ms 2432 KB Output is correct
5 Correct 2 ms 2432 KB Output is correct
6 Correct 2 ms 2432 KB Output is correct
7 Correct 2 ms 2432 KB Output is correct
8 Correct 2 ms 2432 KB Output is correct
9 Correct 2 ms 2432 KB Output is correct
10 Correct 2 ms 2432 KB Output is correct
11 Correct 2 ms 2432 KB Output is correct
12 Correct 3 ms 2432 KB Output is correct
13 Correct 2 ms 2432 KB Output is correct
14 Correct 2 ms 2432 KB Output is correct
15 Correct 3 ms 2432 KB Output is correct
16 Correct 2 ms 2432 KB Output is correct
17 Correct 3 ms 2432 KB Output is correct
18 Correct 2 ms 2432 KB Output is correct
19 Correct 2 ms 2432 KB Output is correct
20 Correct 2 ms 2432 KB Output is correct
21 Correct 2 ms 2432 KB Output is correct
22 Correct 2 ms 2432 KB Output is correct
23 Correct 2 ms 2432 KB Output is correct
24 Correct 2 ms 2432 KB Output is correct
25 Correct 2 ms 2432 KB Output is correct
26 Correct 2 ms 2432 KB Output is correct
27 Correct 3 ms 2432 KB Output is correct
28 Correct 3 ms 2432 KB Output is correct
29 Correct 2 ms 2432 KB Output is correct
30 Correct 2 ms 2432 KB Output is correct
31 Correct 3 ms 2432 KB Output is correct
32 Correct 2 ms 2432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2560 KB Output is correct
2 Correct 3 ms 2560 KB Output is correct
3 Correct 4 ms 2560 KB Output is correct
4 Correct 3 ms 2560 KB Output is correct
5 Correct 3 ms 2560 KB Output is correct
6 Correct 2 ms 2560 KB Output is correct
7 Correct 2 ms 2560 KB Output is correct
8 Correct 4 ms 2560 KB Output is correct
9 Correct 4 ms 2560 KB Output is correct
10 Correct 4 ms 2560 KB Output is correct
11 Correct 2 ms 2560 KB Output is correct
12 Correct 3 ms 2560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 2944 KB Output is correct
2 Correct 12 ms 2944 KB Output is correct
3 Correct 8 ms 3020 KB Output is correct
4 Correct 8 ms 2944 KB Output is correct
5 Correct 9 ms 2944 KB Output is correct
6 Correct 8 ms 2944 KB Output is correct
7 Correct 8 ms 2944 KB Output is correct
8 Correct 9 ms 2944 KB Output is correct
9 Correct 8 ms 2944 KB Output is correct
10 Correct 11 ms 3072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 66 ms 7288 KB Output is correct
2 Correct 66 ms 7544 KB Output is correct
3 Correct 65 ms 7288 KB Output is correct
4 Correct 64 ms 7544 KB Output is correct
5 Correct 83 ms 7452 KB Output is correct
6 Correct 83 ms 7544 KB Output is correct
7 Correct 70 ms 7452 KB Output is correct
8 Correct 100 ms 7500 KB Output is correct
9 Correct 94 ms 7452 KB Output is correct
10 Correct 97 ms 7544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 205 ms 16768 KB Output is correct
2 Correct 200 ms 17276 KB Output is correct
3 Correct 200 ms 17084 KB Output is correct
4 Correct 201 ms 17280 KB Output is correct
5 Correct 290 ms 17232 KB Output is correct
6 Correct 241 ms 17280 KB Output is correct
7 Correct 238 ms 17280 KB Output is correct
8 Correct 378 ms 17276 KB Output is correct
9 Correct 381 ms 17292 KB Output is correct
10 Correct 325 ms 17280 KB Output is correct
11 Correct 214 ms 17156 KB Output is correct
12 Correct 352 ms 17280 KB Output is correct