結果

問題 No.765 ukuku 2
ユーザー pekempeypekempey
提出日時 2018-12-13 02:18:47
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 145 ms / 3,000 ms
コード長 1,982 bytes
コンパイル時間 307 ms
コンパイル使用メモリ 34,928 KB
実行使用メモリ 8,328 KB
最終ジャッジ日時 2023-10-25 09:00:27
合計ジャッジ時間 3,765 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,312 KB
testcase_01 AC 2 ms
5,312 KB
testcase_02 AC 2 ms
5,312 KB
testcase_03 AC 2 ms
5,312 KB
testcase_04 AC 2 ms
5,312 KB
testcase_05 AC 2 ms
5,312 KB
testcase_06 AC 2 ms
5,312 KB
testcase_07 AC 3 ms
5,312 KB
testcase_08 AC 2 ms
5,312 KB
testcase_09 AC 2 ms
5,312 KB
testcase_10 AC 2 ms
5,312 KB
testcase_11 AC 2 ms
5,312 KB
testcase_12 AC 2 ms
5,312 KB
testcase_13 AC 2 ms
5,312 KB
testcase_14 AC 2 ms
5,312 KB
testcase_15 AC 2 ms
5,312 KB
testcase_16 AC 2 ms
5,312 KB
testcase_17 AC 2 ms
5,312 KB
testcase_18 AC 2 ms
5,312 KB
testcase_19 AC 2 ms
5,312 KB
testcase_20 AC 2 ms
5,312 KB
testcase_21 AC 2 ms
5,312 KB
testcase_22 AC 2 ms
5,312 KB
testcase_23 AC 2 ms
5,312 KB
testcase_24 AC 2 ms
5,312 KB
testcase_25 AC 2 ms
5,312 KB
testcase_26 AC 2 ms
5,312 KB
testcase_27 AC 2 ms
5,312 KB
testcase_28 AC 2 ms
5,312 KB
testcase_29 AC 2 ms
5,312 KB
testcase_30 AC 93 ms
8,248 KB
testcase_31 AC 92 ms
8,240 KB
testcase_32 AC 79 ms
8,192 KB
testcase_33 AC 49 ms
8,328 KB
testcase_34 AC 49 ms
8,328 KB
testcase_35 AC 123 ms
8,328 KB
testcase_36 AC 123 ms
8,328 KB
testcase_37 AC 127 ms
8,328 KB
testcase_38 AC 141 ms
8,328 KB
testcase_39 AC 145 ms
8,328 KB
testcase_40 AC 93 ms
8,252 KB
testcase_41 AC 104 ms
8,276 KB
testcase_42 AC 110 ms
8,296 KB
testcase_43 AC 103 ms
8,264 KB
testcase_44 AC 101 ms
8,264 KB
testcase_45 AC 117 ms
8,304 KB
testcase_46 AC 99 ms
8,256 KB
testcase_47 AC 114 ms
8,312 KB
testcase_48 AC 112 ms
8,304 KB
testcase_49 AC 115 ms
8,308 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define M 1000000007

long long B;
int n;
char s[200001];
char t[200001];
long long H[200001];
long long G[200001];
long long P[200001];

int min(int x, int y) {
    return x < y ? x : y;
}

int max(int x, int y) {
    return x > y ? x : y;
}

long long mod(long long x) {
    x %= M;
    if (x < 0) x += M;
    return x;
}

long long f(int l, int n) {
    return mod(H[l + n] - H[l] * P[n]);
}

long long g(int l, int n) {
    return mod(G[l + n] - G[l] * P[n]);
}

int lcp(int a, int b, int n1, int n2) {
    int l = 0;
    int r = min(n1, n2) + 1;
    while (r - l > 1) {
        int mid = (l + r) / 2;
        if (f(a, mid) == g(b, mid)) {
            l = mid;
        } else {
            r = mid;
        }
    }
    return l;
}

int solve(int a, int b, int n1, int n2) {
    int l = 0;
    int r = min(n1, n2) + 1;
    while (r - l > 1) {
        int mid = (l + r) / 2;
        if (f(a, mid) == g(b, mid)) {
            l = mid;
        } else {
            r = mid;
        }
    }
    a += l;
    b += l;
    n1 -= l;
    n2 -= l;
    int vl = lcp(a, b + 1, n1, n2 - 1);
    int vr = lcp(a + 1, b, n1 - 1, n2);
    return l + max(vl, vr);
}

int main() {
    scanf("%s", s);
    n = strlen(s);
    for (int i = 0; i < n; i++) {
        t[n - 1 - i] = s[i];
    }
    srand(time(NULL));
    B = rand() % M;
    P[0] = 1;
    for (int i = 1; i <= n; i++) {
        P[i] = B * P[i - 1] % M;
    }
    for (int i = 0; i < n; i++) {
        H[i + 1] = (H[i] * B + s[i]) % M;
        G[i + 1] = (G[i] * B + t[i]) % M;
    }
    int ans = 0;
    // odd length
    for (int i = 0; i < n; i++) {
        int x = 2 * solve(i + 1, n - i, n - i - 1, i);
        x = min(x, n - 2);
        ans = max(ans, x + 1);
    }
    // even length
    for (int i = 1; i < n; i++) {
        int x  = 2 * solve(i, n - i, n - i, i);
        x = min(x, n - 1);
        ans = max(ans, x);
    }
    printf("%d\n", ans);
}

0