結果

問題 No.765 ukuku 2
ユーザー rogi52rogi52
提出日時 2022-02-22 19:22:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,600 bytes
コンパイル時間 3,547 ms
コンパイル使用メモリ 204,640 KB
実行使用メモリ 16,152 KB
最終ジャッジ日時 2023-09-13 09:55:21
合計ジャッジ時間 14,708 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 WA -
testcase_12 AC 2 ms
4,376 KB
testcase_13 WA -
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 WA -
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 WA -
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 500 ms
13,124 KB
testcase_31 AC 516 ms
12,876 KB
testcase_32 AC 472 ms
11,148 KB
testcase_33 AC 3 ms
4,376 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 794 ms
16,088 KB
testcase_36 WA -
testcase_37 AC 826 ms
16,132 KB
testcase_38 AC 705 ms
16,048 KB
testcase_39 AC 629 ms
16,064 KB
testcase_40 AC 410 ms
13,148 KB
testcase_41 AC 534 ms
14,100 KB
testcase_42 AC 537 ms
14,712 KB
testcase_43 AC 524 ms
13,668 KB
testcase_44 AC 473 ms
13,764 KB
testcase_45 WA -
testcase_46 AC 604 ms
13,420 KB
testcase_47 AC 584 ms
15,232 KB
testcase_48 WA -
testcase_49 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

struct RollingHash {
    static constexpr int M = 2;
    static constexpr long long MODS[M] = {999999937, 1000000007};
    static constexpr long long BASE = 9973;
    vector<long long> powb[M], hash[M];
    int n;
    RollingHash() {}
    RollingHash(const string& str) { init(str); }
    void init(const string& str) {
        n = str.size();
        for (int k = 0; k < M; k++) {
            powb[k].resize(n + 1, 1);
            hash[k].resize(n + 1, 0);
            for (int i = 0; i < n; i++) {
                hash[k][i + 1] = (hash[k][i] * BASE + str[i]) % MODS[k];
                powb[k][i + 1] = powb[k][i] * BASE % MODS[k];
            }
        }
    }
    // get hash str[l,r)
    long long get(int l, int r, int k) {
        long long res = hash[k][r] - hash[k][l] * powb[k][r - l] % MODS[k];
        if (res < 0) res += MODS[k];
        return res;
    }
};

bool match(RollingHash& rh1, int l1, int r1, RollingHash& rh2, int l2, int r2) {
    bool res = true;
    for (int k = 0; k < RollingHash::M; k++) {
        res &= rh1.get(l1, r1, k) == rh2.get(l2, r2, k);
    }
    return res;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    string S; cin >> S;
    int N = S.size();
    string T = S;
    reverse(T.begin(), T.end());

    if(S == T) {
        cout << N - 1 << endl;
        return 0;
    }

    RollingHash HS(S), HT(T);
    int ans = 0;
    rep(i,N) {
        int L = 0, R = min(i, N - (i + 1)) + 1;
        while(R - L > 1) {
            int x = (R + L) / 2;
            (match(HS, i + 1, i + 1 + x, HT, N - i, N - i + x) ? L : R) = x;
        }

        if(L == min(i, N - (i + 1))) {
            ans = max(ans, L + 1 + L);
            continue;
        }

        int SL = L, SR = R;

        {
            L = SL, R = SR;
            int pos = L;
            R = min(i, N - (i + 2)) + 1;
            while(R - L > 1) {
                int x = (R + L) / 2;
                (match(HS, i + 2 + pos, i + 2 + x, HT, N - i + pos, N - i + x) ? L : R) = x;
            }
            ans = max(ans, L + 1 + L);
        }

        {
            L = SL, R = SR;
            int pos = L;
            R = min(i - 1, N - (i + 1)) + 1;
            while(R - L > 1) {
                int x = (R + L) / 2;
                (match(HS, i + pos, i + 1 + x, HT, N - i + pos + 1, N - i + x + 1) ?  L : R) = x;
            }
            ans = max(ans, L + 1 + L);
        }
    }

    rep(i,N-1) if(S[i] == S[i + 1]) {
        int L = 0, R = min(i, N - (i + 2)) + 1;
        while(R - L > 1) {
            int x = (R + L) / 2;
            (match(HS, i + 2, i + 2 + x, HT, N - i, N - i + x) ? L : R) = x;
        }

        if(L == min(i, N - (i + 2))) {
            ans = max(ans, L + 2 + L);
            continue;
        }

        int SL = L, SR = R;

        {
            L = SL, R = SR;
            int pos = L;
            R = min(i, N - (i + 3)) + 1;
            while(R - L > 1) {
                int x = (R + L) / 2;
                (match(HS, i + 3 + pos, i + 3 + x, HT, N - i + pos, N - i + x) ? L : R) = x;
            }
            ans = max(ans, L + 2 + L);
        }

        {
            L = SL, R = SR;
            int pos = L;
            R = min(i - 1, N - (i + 2)) + 1;
            while(R - L > 1) {
                int x = (R + L) / 2;
                (match(HS, i + 2 + pos, i + 2 + x, HT, N - i + pos + 1, N - i + x + 1) ? L : R) = x;
            }
            ans = max(ans, L + 2 + L);
        }
    }

    cout << ans << endl;
}
0