結果

問題 No.765 ukuku 2
ユーザー parukiparuki
提出日時 2018-12-25 09:40:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 282 ms / 3,000 ms
コード長 2,651 bytes
コンパイル時間 1,813 ms
コンパイル使用メモリ 175,144 KB
実行使用メモリ 16,384 KB
最終ジャッジ日時 2024-04-08 22:20:52
合計ジャッジ時間 8,482 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 1 ms
6,548 KB
testcase_04 AC 2 ms
6,548 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 2 ms
6,548 KB
testcase_07 AC 2 ms
6,548 KB
testcase_08 AC 2 ms
6,548 KB
testcase_09 AC 2 ms
6,548 KB
testcase_10 AC 2 ms
6,548 KB
testcase_11 AC 2 ms
6,548 KB
testcase_12 AC 2 ms
6,548 KB
testcase_13 AC 2 ms
6,548 KB
testcase_14 AC 2 ms
6,548 KB
testcase_15 AC 2 ms
6,548 KB
testcase_16 AC 2 ms
6,548 KB
testcase_17 AC 2 ms
6,548 KB
testcase_18 AC 2 ms
6,548 KB
testcase_19 AC 3 ms
6,548 KB
testcase_20 AC 2 ms
6,548 KB
testcase_21 AC 2 ms
6,548 KB
testcase_22 AC 2 ms
6,548 KB
testcase_23 AC 1 ms
6,548 KB
testcase_24 AC 2 ms
6,548 KB
testcase_25 AC 2 ms
6,548 KB
testcase_26 AC 2 ms
6,548 KB
testcase_27 AC 2 ms
6,548 KB
testcase_28 AC 2 ms
6,548 KB
testcase_29 AC 2 ms
6,548 KB
testcase_30 AC 183 ms
13,440 KB
testcase_31 AC 178 ms
13,184 KB
testcase_32 AC 147 ms
11,520 KB
testcase_33 AC 98 ms
16,384 KB
testcase_34 AC 97 ms
16,384 KB
testcase_35 AC 241 ms
16,384 KB
testcase_36 AC 239 ms
16,384 KB
testcase_37 AC 256 ms
16,384 KB
testcase_38 AC 277 ms
16,384 KB
testcase_39 AC 282 ms
16,384 KB
testcase_40 AC 183 ms
13,568 KB
testcase_41 AC 198 ms
14,336 KB
testcase_42 AC 219 ms
15,104 KB
testcase_43 AC 193 ms
13,952 KB
testcase_44 AC 198 ms
14,080 KB
testcase_45 AC 226 ms
15,232 KB
testcase_46 AC 191 ms
13,824 KB
testcase_47 AC 227 ms
15,616 KB
testcase_48 AC 215 ms
15,360 KB
testcase_49 AC 224 ms
15,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define MT make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define RT return
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;

struct RollingHash {
    static const int C = 31, C2 = 29, P = 1000000007, P2 = 1000000009;
    int _n;
    vector<long long> pw, pw2, hs, hs2;

    RollingHash() {}
    RollingHash(const string &st)
        :_n((int)st.size()), pw(_n + 1), pw2(_n + 1), hs(_n + 1), hs2(_n + 1)
    {
        pw[0] = pw2[0] = 1;
        rep(i, _n) {
            pw[i + 1] = (pw[i] * C) % P;
            pw2[i + 1] = (pw2[i] * C2) % P2;
            hs[i + 1] = (C*hs[i] + st[i]) % P;
            hs2[i + 1] = (C2*hs2[i] + st[i]) % P2;
        }
    }

    // [l, r)
    pii getHash(int l, int r) {
        int res, res2;
        res = (hs[r] - hs[l] * pw[r - l]) % P;
        res2 = (hs2[r] - hs2[l] * pw2[r - l]) % P2;
        if (res < 0)res += P;
        if (res2 < 0)res2 += P2;
        return mp(res, res2);
    }
};

string S;
int N;

void solve() {
    cin >> S;
    N = sz(S);
    auto T = S;
    reverse(all(T));
    RollingHash hsh(S), rev(T);

    auto f = [&](int ml, int mr, int x) {
        auto p = hsh.getHash(ml - x, ml);
        auto q = rev.getHash(N - (mr + x), N - mr);
        return p == q;
    };

    int ans = 0;
    rep(ml, N) FOR(mr, ml, ml + 2) {
        int ok = 0, ng = min(ml, N - mr) + 1;
        while (ng - ok > 1) {
            int x = (ok + ng) / 2;
            (f(ml, mr, x) ? ok : ng) = x;
        }
        int l = ml - ok, r = mr + ok;
        if (r - l == N)continue;
        smax(ans, r - l);

        if (l > 1) {
            ok = 0, ng = min(l - 1, N - r) + 1;
            while (ng - ok > 1) {
                int x = (ok + ng) / 2;
                (f(l - 1, r, x) ? ok : ng) = x;
            }
            smax(ans, r - l + ok * 2);
        }
        if (r < N) {
            ok = 0, ng = min(l, N - (r + 1)) + 1;
            while (ng - ok > 1) {
                int x = (ok + ng) / 2;
                (f(l, r + 1, x) ? ok : ng) = x;
            }
            smax(ans, r - l + ok * 2);
        }
    }
    cout << ans << endl;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(15);
	solve();
	return 0;
}
0