結果

問題 No.765 ukuku 2
ユーザー りあんりあん
提出日時 2018-12-13 00:46:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 994 ms / 3,000 ms
コード長 4,101 bytes
コンパイル時間 2,614 ms
コンパイル使用メモリ 215,060 KB
実行使用メモリ 12,944 KB
最終ジャッジ日時 2024-09-25 03:49:36
合計ジャッジ時間 17,402 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 2 ms
6,944 KB
testcase_26 AC 3 ms
6,944 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 2 ms
6,940 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 617 ms
11,584 KB
testcase_31 AC 571 ms
11,432 KB
testcase_32 AC 475 ms
8,784 KB
testcase_33 AC 605 ms
12,848 KB
testcase_34 AC 600 ms
12,876 KB
testcase_35 AC 982 ms
12,840 KB
testcase_36 AC 994 ms
12,648 KB
testcase_37 AC 619 ms
12,776 KB
testcase_38 AC 628 ms
12,872 KB
testcase_39 AC 674 ms
12,944 KB
testcase_40 AC 643 ms
11,692 KB
testcase_41 AC 695 ms
11,960 KB
testcase_42 AC 729 ms
12,336 KB
testcase_43 AC 627 ms
11,724 KB
testcase_44 AC 605 ms
11,868 KB
testcase_45 AC 689 ms
12,476 KB
testcase_46 AC 583 ms
11,752 KB
testcase_47 AC 685 ms
12,420 KB
testcase_48 AC 684 ms
12,444 KB
testcase_49 AC 665 ms
12,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
const int M = 1000000007;

class suffix_array {
    // compare (rank[i], rank[i + k]) and (rank[j], rank[j + k])
    static bool compare_sa(int n, const vector<int>& rank, int i, int j, int k) {
        if (rank[i] != rank[j]) return rank[i] < rank[j];
        int ri = i + k <= n ? rank[i + k] : -1;
        int rj = j + k <= n ? rank[j + k] : -1;
        return ri < rj;
    }
public:
    static vector<int> construct_sa(const string& s) {
        int n = s.length();
        vector<int> sa(n + 1), rank(n + 1);
        for (int i = 0; i <= n; ++i) {
            sa[i] = i;
            rank[i] = i < n ? s[i] : -1;
        }
        for (int k = 1; k <= n; k <<= 1) {
            sort(sa.begin(), sa.end(), [&n, &k, &rank](const int& a, const int& b){ return compare_sa(n, rank, a, b, k); });

            vector<int> tmp(n + 1);
            for (int i = 1; i <= n; ++i)
                tmp[sa[i]] = tmp[sa[i - 1]] + compare_sa(n, rank, sa[i - 1], sa[i], k);
            for (int i = 0; i <= n; ++i)
                rank[i] = tmp[i];
        }
        return sa;
    }
    static vector<int> construct_lcp(const string& s, const vector<int>& sa) {
        int n = s.length();
        vector<int> rank(n + 1), lcp(n + 1);
        for (int i = 0; i <= n; ++i) rank[sa[i]] = i;

        int h = 0;
        for (int i = 0; i < n; ++i) {
            if (h > 0) --h;
            for (int j = sa[rank[i] - 1]; j + h < n && i + h < n; ++h)
                if (s[j + h] != s[i + h]) break;

            lcp[rank[i] - 1] = h;
        }
        return lcp;
    }
};

class segtree {
private:
    int n, s, t;
    vector<int> tr;
    const int ex = M;

    int q(int k, int l, int r) {
        return r <= s || t <= l ? ex : s <= l && r <= t ? tr[k]
                : min(q(k << 1 | 1, l, (l + r) >> 1), q((k + 1) << 1, (l + r) >> 1, r));
    }

public:
    segtree(int m) {
        n = 1;
        while (n < m) n <<= 1;
        tr = vector<int>((n << 1) - 1, ex);
    }
    void update(int j, const int& x) {
        int i = j + n - 1;
        tr[i] = x;
        while (i > 0) { i = (i - 1) >> 1; tr[i] = min(tr[i << 1 | 1], tr[(i + 1) << 1]); }
    }
    // [s, t)
    int run(int _s, int _t) { s = _s; t = _t; return q(0, 0, n); }
};


int main() {
    string s;
    cin >> s;
    int n = s.length();
    int m = n * 2 + 1;
    string rev = s;
    reverse(rev.begin(), rev.end());
    string ss = s + "#" + rev;
    vector<int> sa = suffix_array::construct_sa(ss);
    vector<int> lcp = suffix_array::construct_lcp(ss, sa);
    vector<int> rank(m + 1);
    segtree sg(m + 1);
    for (int i = 0; i <= m; ++i) {
        sg.update(i, lcp[i]);
        // cout << lcp[i] << "\n";
        rank[sa[i]] = i;
    }
    int ans = 1;
    for (int i = 1; i < n; ++i) {
        {
            int ri = i, li = m - i;
            int len = sg.run(min(rank[ri], rank[li]), max(rank[ri], rank[li]));
            ri += len;
            li += len;
            ans = max(ans, len * 2 - 2);
            if (ri <= n && li + 1 <= m) {
                int len1 = sg.run(min(rank[ri], rank[li + 1]), max(rank[ri], rank[li + 1]));
                ans = max(ans, len * 2 + len1 * 2);
            }
            if (ri + 1 <= n && li <= m) {
                int len1 = sg.run(min(rank[ri + 1], rank[li]), max(rank[ri + 1], rank[li]));
                ans = max(ans, len * 2 + len1 * 2);
            }
        }
        {
            int ri = i, li = m - i + 1;
            int len = sg.run(min(rank[ri], rank[li]), max(rank[ri], rank[li]));
            ri += len;
            li += len;
            ans = max(ans, len * 2);
            if (ri <= n && li + 1 <= m) {
                int len1 = sg.run(min(rank[ri], rank[li + 1]), max(rank[ri], rank[li + 1]));
                ans = max(ans, len * 2 + len1 * 2 + 1);
            }
            if (ri + 1 <= n && li <= m) {
                int len1 = sg.run(min(rank[ri + 1], rank[li]), max(rank[ri + 1], rank[li]));
                ans = max(ans, len * 2 + len1 * 2 + 1);
            }
        }
    }
    cout << ans << "\n";
    return 0;
}
0