結果

問題 No.765 ukuku 2
ユーザー treeonetreeone
提出日時 2018-11-25 22:03:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 888 ms / 3,000 ms
コード長 2,616 bytes
コンパイル時間 2,374 ms
コンパイル使用メモリ 211,844 KB
実行使用メモリ 41,560 KB
最終ジャッジ日時 2023-08-26 19:59:44
合計ジャッジ時間 17,165 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,384 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,384 KB
testcase_24 AC 2 ms
4,376 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 2 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 588 ms
32,748 KB
testcase_31 AC 576 ms
33,228 KB
testcase_32 AC 482 ms
25,356 KB
testcase_33 AC 288 ms
37,636 KB
testcase_34 AC 287 ms
37,644 KB
testcase_35 AC 781 ms
40,500 KB
testcase_36 AC 784 ms
40,292 KB
testcase_37 AC 794 ms
40,764 KB
testcase_38 AC 882 ms
40,500 KB
testcase_39 AC 888 ms
41,560 KB
testcase_40 AC 598 ms
33,580 KB
testcase_41 AC 659 ms
36,124 KB
testcase_42 AC 706 ms
38,404 KB
testcase_43 AC 641 ms
33,920 KB
testcase_44 AC 641 ms
34,208 KB
testcase_45 AC 723 ms
37,432 KB
testcase_46 AC 628 ms
33,968 KB
testcase_47 AC 730 ms
38,668 KB
testcase_48 AC 716 ms
39,432 KB
testcase_49 AC 720 ms
37,680 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++){
        len[i]--;
        left[i] = i - len[i];
        right[i] = i + len[i];
    }
    string sr = s;
    reverse(sr.begin(), sr.end());
    string ssr = s + "#" + sr;
    RollingHash rh(ssr);
	vector<int> d;
    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);
			d.push_back(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);
			d.push_back(len[i] + lcp);
        }
    }
	sort(d.rbegin(), d.rend());
	ans = d[0];
    cout << ans << endl;
}
0