結果

問題 No.765 ukuku 2
ユーザー xuzijian629xuzijian629
提出日時 2018-12-13 02:57:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,357 bytes
コンパイル時間 2,498 ms
コンパイル使用メモリ 209,820 KB
実行使用メモリ 18,672 KB
最終ジャッジ日時 2023-10-25 09:01:30
合計ジャッジ時間 8,625 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 3 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 WA -
testcase_04 AC 2 ms
4,348 KB
testcase_05 WA -
testcase_06 AC 2 ms
4,348 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 3 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 WA -
testcase_12 AC 2 ms
4,348 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 3 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 3 ms
4,348 KB
testcase_18 AC 3 ms
4,348 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 4 ms
4,348 KB
testcase_23 WA -
testcase_24 AC 4 ms
4,348 KB
testcase_25 AC 3 ms
4,348 KB
testcase_26 AC 7 ms
4,348 KB
testcase_27 AC 6 ms
4,348 KB
testcase_28 AC 7 ms
4,348 KB
testcase_29 AC 6 ms
4,348 KB
testcase_30 TLE -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using vi = vector<i64>;
using vvi = vector<vi>;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

i64 modpow(i64 a, i64 n, i64 mod) {
    if (n == 0) return 1;
    if (n % 2 == 0) {
        i64 t = modpow(a, n / 2, mod);
        return t * t % mod;
    }
    return a * modpow(a, n - 1, mod) % mod;
}

i64 modinv(i64 a, i64 mod) {
    return modpow(a, mod - 2, mod);
}

bool is_prime(i64 n, int k = 50) {
    if (n == 2) return true;
    if (n % 2 == 0 || n < 2) return false;
    i64 d = n - 1;
    while (d % 2 == 0) {
        d /= 2;
    }
    for (int i = 0; i < k; i++) {
        i64 a = rnd() % (n - 2) + 1;
        i64 t = d;
        i64 y = modpow(a, t, n);
        while (t != n - 1 && y != 1 && y != n - 1) {
            y = modpow(y, 2, n);
            t *= 2;
        }
        if (y != n - 1 && t % 2 == 0) {
            return false;
        }
    }
    return true;
}

i64 gen_prime() {
    while (1) {
        i64 d = rnd() & ((i64(1) << 30) - 1);
        d |= 1;
        if (is_prime(d)) {
            return d;
        }
    }
}

struct RollingHash {
    vi hash;
    vi pows;
    i64 p, m;
    RollingHash(string s, i64 p = gen_prime(), i64 m = gen_prime()) : hash(s.size() + 1), pows(s.size() + 1), p(p), m(m) {
        hash[0] = 1;
        pows[0] = 1;
        for (int i = 0; i < s.size(); i++) {
            hash[i + 1] = (hash[i] * p % m + s[i]) % m;
            pows[i + 1] = pows[i] * p % m;
        }
    }
    
    // [l, r)
    i64 encode(int l, int r) {
        return ((hash[r] - hash[l] * pows[r - l] % m) % m + m) % m;
    }
    
    // [l, r)のうち[ml, mr)をスキップした文字列のencode
    i64 skipencode(int l, int r, int ml, int mr) {
        assert(l <= ml && ml <= mr && mr <= r);
        return (encode(l, ml) * pows[r - mr] % m + encode(mr, r)) % m;
    }
    
    bool eq(int l0, int r0, int l1, int r1) {
        return encode(l0, r0) == encode(l1, r1);
    }
};

int main() {
    string S;
    cin >> S;
    string s = "$";
    for (char c : S) {
        s += c;
        s += '$';
    }
    int n = s.size();
    string t(s);
    reverse(t.begin(), t.end());
    i64 p = gen_prime(), p2 = gen_prime();
    RollingHash rh(s, p, p2), rh2(t, p, p2);
    int nax = 1;
    for (int i = 1; i < n - 1; i++) {
        cerr << "i = " << i << endl;
        int l = 0, r = min(i, n - 1 - i) + 1;
        auto ok = [&](int m) {
            return rh.encode(i + 1, i + 1 + m) == rh2.encode(n - i, n - i + m);
        };
        while (l < r - 1) {
            int m = (l + r) / 2;
            if (ok(m)) {
                l = m;
            } else {
                r = m;
            }
        }
        int K = l;

        cerr << s << endl;
        cerr << "when center is " << i << ", longest common LR = " << l << endl;

        if (n - 1 - i == l && i == l) {
            nax = max(nax, l - 2);
            continue;
        }

        if (n - 1 - i == l) {
            nax = max(nax, l);
        } else {
            r = min(i, n - 1 - i) + 1;
            auto ok2 = [&](int m) {
                if (i + 1 + m + 2 >= n) return false;
                return rh.skipencode(i + 1, i + 1 + m + 2, i + 1 + K, i + 1 + K + 2) == rh2.encode(n - i, n - i + m);
            };

            while (l < r - 1) {
                int m = (l + r) / 2;
                if (ok2(m)) {
                    l = m;
                } else {
                    r = m;
                }
            }

            cerr << "when erase right, the longest commond LR = " << l << endl;

            nax = max(nax, l);
        }

        l = K;

        if (i == l) {
            nax = max(nax, l - 1);
        } else {
            r = min(i, n - 1 - i) + 1;
            auto ok3 = [&](int m) {
                if (n - i + m + 2 >= n) return false;
                return rh.encode(i + 1, i + 1 + m) == rh2.skipencode(n - i, n - i + m + 2, n - i + K, n - i + K + 2);
            };

            while (l < r - 1) {
                int m = (l + r) / 2;
                if (ok3(m)) {
                    l = m;
                } else {
                    r = m;
                }
            }

            cerr << "when erase left, the longest commond LR = " << l << endl;

            nax = max(nax, l);
        }
    }
    cout << nax << endl;
}
0