結果

問題 No.765 ukuku 2
ユーザー treeonetreeone
提出日時 2018-10-29 03:39:17
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 852 ms / 3,000 ms
コード長 2,626 bytes
コンパイル時間 1,750 ms
コンパイル使用メモリ 173,128 KB
実行使用メモリ 35,048 KB
最終ジャッジ日時 2023-08-26 19:56:07
合計ジャッジ時間 16,580 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 563 ms
27,636 KB
testcase_31 AC 554 ms
26,996 KB
testcase_32 AC 461 ms
23,064 KB
testcase_33 AC 246 ms
34,856 KB
testcase_34 AC 245 ms
34,756 KB
testcase_35 AC 755 ms
34,736 KB
testcase_36 AC 755 ms
34,752 KB
testcase_37 AC 767 ms
34,888 KB
testcase_38 AC 846 ms
35,048 KB
testcase_39 AC 852 ms
34,952 KB
testcase_40 AC 575 ms
27,968 KB
testcase_41 AC 632 ms
29,924 KB
testcase_42 AC 674 ms
31,768 KB
testcase_43 AC 608 ms
28,860 KB
testcase_44 AC 609 ms
29,224 KB
testcase_45 AC 689 ms
31,932 KB
testcase_46 AC 600 ms
28,508 KB
testcase_47 AC 696 ms
32,888 KB
testcase_48 AC 681 ms
32,456 KB
testcase_49 AC 687 ms
32,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct RollingHash {
    int n;
    long long base1, base2, mod1, mod2;
    vector<long long> hash1, hash2, pow1, pow2;
    RollingHash(const string &s) : base1(1009), base2(1007), mod1(1000000007), mod2(1000000009) {
        n = s.size();
        hash1.assign(n + 1, 0); hash2.assign(n + 1, 0); pow1.assign(n + 1, 1); pow2.assign(n + 1, 1);
        for(int i = 0; i < n; i++) {
            hash1[i + 1] = (hash1[i] + s[i]) * base1 % mod1;
            hash2[i + 1] = (hash2[i] + s[i]) * base2 % mod2;
            pow1[i + 1] = pow1[i] * base1 % mod1;
            pow2[i + 1] = pow2[i] * base2 % mod2;
        }
    }
    pair<long long, long long> get(int l, int r) {
        long long t1 = ((hash1[r] - hash1[l] * pow1[r - l]) % mod1 + mod1) % mod1;
        long long t2 = ((hash2[r] - hash2[l] * pow2[r - l]) % mod2 + mod2) % mod2;
        return {t1, t2};
    }
    int LCP(int l, int r){
        int len = n - r, low = -1, high = len + 1;
        while(high - low > 1){
            int mid = (low + high) / 2;
            if(get(l, l + mid) == get(r, r + mid)) low = mid;
            else high = mid;
        }
        return low;
    }
};

vector<int> manacher(string &s){
    int i = 0, j = 0;
    vector<int> res(s.size());
    while (i < s.size()) {
        while (i - j >= 0 && i + j < s.size() && s[i - j] == s[i + j]) ++j;
        res[i] = j;
        int k = 1;
        while (i - k >= 0 && i + k < s.size() && k + res[i - k] < j) res[i + k] = res[i - k], ++k;
        i += k; j -= k;
    }
    return res;
}

int main(){
    int ans = 0;
    string s, t = "$";
    cin >> s;
    for(int i = 0; i < s.size(); i++){
        t += s[i]; t += "$";
    }
    s = t;
    vector<int> len = manacher(s), left(s.size()), right(s.size());
    for(int i = 0; i < len.size(); i++){
        left[i] = i - len[i] + 1;
        right[i] = i + len[i] - 1;
        len[i]--;
        ans = max(ans, len[i] - 1);
    }
    string sr = s;
    reverse(sr.begin(), sr.end());
    string ssr = s + "#" + sr;
    RollingHash rh(ssr);
    for(int i = 0; i < s.size(); i++){
        if(right[i] + 1 <= s.size() - 1){
            int rightStart = right[i] + 3;
            int leftStart = ssr.size() - 1 - (left[i] - 1);
            int lcp = rh.LCP(rightStart, leftStart);
            ans = max(ans, len[i] + lcp);
        }
        if(left[i] - 1 >= 0){
            int rightStart = right[i] + 1;
            int leftStart = ssr.size() - 1 - (left[i] - 3);
            int lcp = rh.LCP(rightStart, leftStart);
            ans = max(ans, len[i] + lcp);
        }
    }
    cout << ans << endl;
}
0