結果

問題 No.765 ukuku 2
ユーザー PachicobuePachicobue
提出日時 2018-12-13 01:33:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 175 ms / 3,000 ms
コード長 3,225 bytes
コンパイル時間 2,359 ms
コンパイル使用メモリ 212,012 KB
実行使用メモリ 31,772 KB
最終ジャッジ日時 2023-10-25 08:51:07
合計ジャッジ時間 5,987 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 105 ms
25,436 KB
testcase_31 AC 103 ms
24,608 KB
testcase_32 AC 88 ms
21,200 KB
testcase_33 AC 29 ms
31,772 KB
testcase_34 AC 29 ms
31,772 KB
testcase_35 AC 136 ms
31,772 KB
testcase_36 AC 136 ms
31,772 KB
testcase_37 AC 143 ms
31,772 KB
testcase_38 AC 166 ms
31,772 KB
testcase_39 AC 175 ms
31,772 KB
testcase_40 AC 105 ms
25,720 KB
testcase_41 AC 117 ms
27,412 KB
testcase_42 AC 127 ms
29,020 KB
testcase_43 AC 115 ms
26,356 KB
testcase_44 AC 113 ms
26,568 KB
testcase_45 AC 127 ms
29,304 KB
testcase_46 AC 113 ms
26,076 KB
testcase_47 AC 130 ms
30,148 KB
testcase_48 AC 126 ms
29,600 KB
testcase_49 AC 128 ms
29,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using ll = long long;
std::mt19937 mt{std::random_device{}()};
template <typename C = std::string, ll mod1 = 1000000007LL, ll mod2 = 1000000009LL>
class RollingHash
{
public:
    RollingHash(const C& s) : pow1(s.size() + 1, 1), pow2(s.size() + 1, 1), hash1(s.size() + 1, 0), hash2(s.size() + 1, 0), base1{std::uniform_int_distribution<ll>{2, mod1 - 1}(mt)}, base2{std::uniform_int_distribution<ll>{2, mod2 - 1}(mt)}
    {
        for (std::size_t i = 1; i <= s.size(); i++) { pow1[i] = (pow1[i - 1] * base1) % mod1, pow2[i] = (pow2[i - 1] * base2) % mod2, hash1[i] = (hash1[i - 1] * base1 + (ll)s[i - 1]) % mod1, hash2[i] = (hash2[i - 1] * base2 + (ll)s[i - 1]) % mod2; }
    }
    std::pair<ll, ll> getHash(const std::size_t l, const std::size_t r) const { return {(mod1 + (hash1[r] - hash1[l] * pow1[r - l]) % mod1) % mod1, (mod2 + (hash2[r] - hash2[l] * pow2[r - l]) % mod2) % mod2}; }

private:
    std::vector<ll> pow1, pow2, hash1, hash2;
    const ll base1, base2;
};
std::vector<int> Manacher(const std::string& S)
{
    std::vector<int> R(S.size());
    for (int i = 0, k = 1, j = 0; i < S.size(); i += k, j -= k, k = 1) {
        while (i >= j and i + j < S.size() and S[i - j] == S[i + j]) { ++j; }
        R[i] = j;
        while (i >= k and i + k < S.size() and k + R[i - k] < j) { R[i + k] = R[i - k], ++k; }
    }
    return R;
}
int main()
{
    std::string s;
    std::cin >> s;
    const int N = s.size();
    std::string S;
    for (const char c : s) { S.push_back(c), S.push_back('$'); }
    S.pop_back();
    std::string R = S;
    std::reverse(R.begin(), R.end());
    const std::string T = S + "#" + R;
    RollingHash<> hash(T);
    const auto m = Manacher(S);
    int max = 0;
    for (int i = 0; i < S.size(); i++) {
        if (i - m[i] == -1 and i + m[i] == S.size()) { return std::cout << N - 1 << std::endl, 0; }
        if (i - m[i] == -1 or i + m[i] == S.size()) {
            max = std::max(max, i % 2 == 1 ? m[i] / 2 * 2 : (m[i] + 1) / 2 + (m[i] - 1) / 2);
            continue;
        }
        {
            int inf = -1, sup = std::min((int)S.size() - (i + m[i]) - 1, i - m[i] + 2);
            while (sup - inf > 1) {
                const int mid = (inf + sup) / 2;
                const auto l = hash.getHash(2 * S.size() - i + m[i], 2 * S.size() - i + m[i] + mid);
                const auto r = hash.getHash(i + m[i] + 2, i + m[i] + mid + 2);
                (l == r ? inf : sup) = mid;
            }
            const int len = m[i] + inf;
            max = std::max(max, i % 2 == 1 ? len / 2 * 2 : (len + 1) / 2 + (len - 1) / 2);
        }
        {
            int inf = -1, sup = std::min((int)S.size() - (i + m[i]) + 1, i - m[i]);
            while (sup - inf > 1) {
                const int mid = (inf + sup) / 2;
                const auto l = hash.getHash(2 * S.size() - i + m[i] + 2, 2 * S.size() - i + m[i] + 2 + mid);
                const auto r = hash.getHash(i + m[i], i + m[i] + mid);
                (l == r ? inf : sup) = mid;
            }
            const int len = m[i] + inf;
            max = std::max(max, i % 2 == 1 ? len / 2 * 2 : (len + 1) / 2 + (len - 1) / 2);
        }
    }
    std::cout << max << std::endl;
    return 0;
}
0