結果

問題 No.765 ukuku 2
ユーザー nebukuro09nebukuro09
提出日時 2018-12-13 14:06:35
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 447 ms / 3,000 ms
コード長 2,464 bytes
コンパイル時間 813 ms
コンパイル使用メモリ 104,004 KB
実行使用メモリ 10,620 KB
最終ジャッジ日時 2023-09-03 21:39:56
合計ジャッジ時間 9,104 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,388 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 1 ms
4,384 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,384 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,384 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,384 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,384 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,384 KB
testcase_24 AC 1 ms
4,384 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,384 KB
testcase_27 AC 1 ms
4,384 KB
testcase_28 AC 2 ms
4,384 KB
testcase_29 AC 2 ms
4,384 KB
testcase_30 AC 314 ms
10,620 KB
testcase_31 AC 306 ms
8,836 KB
testcase_32 AC 257 ms
7,396 KB
testcase_33 AC 15 ms
4,380 KB
testcase_34 AC 15 ms
4,380 KB
testcase_35 AC 420 ms
10,176 KB
testcase_36 AC 420 ms
10,108 KB
testcase_37 AC 407 ms
10,408 KB
testcase_38 AC 435 ms
10,344 KB
testcase_39 AC 447 ms
10,136 KB
testcase_40 AC 320 ms
9,576 KB
testcase_41 AC 351 ms
9,064 KB
testcase_42 AC 378 ms
9,648 KB
testcase_43 AC 338 ms
9,580 KB
testcase_44 AC 340 ms
9,188 KB
testcase_45 AC 386 ms
9,608 KB
testcase_46 AC 333 ms
9,592 KB
testcase_47 AC 389 ms
9,728 KB
testcase_48 AC 381 ms
9,596 KB
testcase_49 AC 384 ms
10,196 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;

const long MOD = 10^^9 + 9;
const long BASE = 31;

void main() {
    auto S = readln.chomp;
    auto N = S.length.to!int;
    auto T = S.to!(dchar[]).reverse.to!string;

    if (is_kaibun(S)) {
        writeln(N-1);
        return;
    }

    auto P = new long[](N+1);
    auto P_INV = new long[](N+1);
    P[0] = P_INV[0] = 1;
    foreach (i; 0..N) P[i+1] = P[i] * BASE % MOD;
    foreach (i; 0..N) P_INV[i] = powmod(P[i], MOD-2, MOD);

    auto rh = new long[](N+1);
    rh[N] = 0;
    foreach (i; 0..N) rh[i+1] = (rh[i] + (S[i] - 'a') * P[i]) % MOD;

    auto hr = new long[](N+1);
    hr[N] = 0;
    foreach (i; 0..N) hr[i+1] = (hr[i] + (T[i] - 'a') * P[i]) % MOD;

    long hash_substr(int l, int r) { // [l, r]
        return ((rh[r+1] - rh[l]) * P_INV[l] % MOD + MOD) % MOD;
    }

    long hash_reverse(int l, int r) {
        int nl = N - r - 1;
        int nr = N - l - 1;
        return ((hr[nr+1] - hr[nl]) * P_INV[nl] % MOD + MOD) % MOD;
    }

    int bin_search(int r1, int l2) {
        if (r1 < 0 || l2 >= N)
            return -1;
        int hi = min(r1+1, N-l2);
        int lo = -1;
        while (hi - lo > 1) {
            int mid = (hi + lo) / 2;
            int l1 = r1 - mid;
            int r2 = l2 + mid;
            if (hash_substr(l1, r1) == hash_reverse(l2, r2))
                lo = mid;
            else
                hi = mid;
        }
        return lo;
    }


    int ans = 0;

    foreach (i; 0..N) { // S[i]が中心
        int len1 = bin_search(i-1, i+1) + 1;
        int r1 = i - len1 - 1;
        int l2 = i + len1 + 1;
        int len2 = max(bin_search(r1-1, l2), bin_search(r1, l2+1)) + 1;
        ans = max(ans, len1 * 2 + len2 * 2 + 1);
    }

    foreach (i; 0..N-1) { // S[i..i+1]が中心
        int len1 = bin_search(i, i+1) + 1;
        int r1 = i - len1;
        int l2 = i + len1 + 1;
        int len2 = max(bin_search(r1-1, l2), bin_search(r1, l2+1)) + 1;
        ans = max(ans, len1 * 2 + len2 * 2);
    }


    ans.writeln;
}

bool is_kaibun(string s) {
    return iota(s.length/2).map!(i => s[i] == s[s.length.to!int-i-1]).all;
}

long powmod(long a, long x, long m) {
    long ret = 1;
    while (x) {
        if (x % 2) ret = ret * a % m;
        a = a * a % m;
        x /= 2;
    }
    return ret;
}
0