結果

問題 No.2204 Palindrome Splitting (No Rearrangement ver.)
ユーザー meruuu61779999meruuu61779999
提出日時 2023-06-17 13:21:56
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,597 bytes
コンパイル時間 978 ms
コンパイル使用メモリ 87,032 KB
実行使用メモリ 157,804 KB
最終ジャッジ日時 2023-09-07 10:06:59
合計ジャッジ時間 29,306 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
71,912 KB
testcase_01 AC 90 ms
72,512 KB
testcase_02 AC 93 ms
71,876 KB
testcase_03 AC 618 ms
80,428 KB
testcase_04 AC 273 ms
80,004 KB
testcase_05 AC 237 ms
80,184 KB
testcase_06 AC 923 ms
80,156 KB
testcase_07 AC 835 ms
80,636 KB
testcase_08 AC 769 ms
80,400 KB
testcase_09 AC 804 ms
80,312 KB
testcase_10 AC 927 ms
80,496 KB
testcase_11 AC 770 ms
80,280 KB
testcase_12 AC 903 ms
80,452 KB
testcase_13 AC 917 ms
80,464 KB
testcase_14 AC 911 ms
80,816 KB
testcase_15 AC 852 ms
80,552 KB
testcase_16 AC 463 ms
80,472 KB
testcase_17 AC 547 ms
80,496 KB
testcase_18 AC 911 ms
80,708 KB
testcase_19 AC 921 ms
80,460 KB
testcase_20 AC 911 ms
80,660 KB
testcase_21 AC 912 ms
80,260 KB
testcase_22 AC 916 ms
80,636 KB
testcase_23 AC 911 ms
80,356 KB
testcase_24 AC 909 ms
80,156 KB
testcase_25 AC 925 ms
80,308 KB
testcase_26 AC 919 ms
80,628 KB
testcase_27 AC 668 ms
80,428 KB
testcase_28 AC 915 ms
80,480 KB
testcase_29 AC 912 ms
80,212 KB
testcase_30 RE -
testcase_31 AC 94 ms
72,448 KB
testcase_32 AC 951 ms
157,468 KB
testcase_33 AC 902 ms
80,292 KB
testcase_34 AC 95 ms
72,152 KB
testcase_35 AC 1,033 ms
157,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import random

"""RollingHash

・つかい方
    rh = RollingHash(S): インスタンス生成
    rh.get(l, r): hash(S[l, r))

・めも
    1つ目のインスタンス生成で係数cfを決めてしまうから、
    たくさんインスタンスつくっても整合性がとれてる。

・参考URL
    https://qiita.com/keymoon/items/11fac5627672a6d6a9f6
"""


class RollingHash:
    def __init__(self, S: str):
        le = len(S)
        if RConst.cf == 0:
            RConst.cf = random.randrange(1 << 31, RConst.Mod)
        if len(RConst.rui_cf) < le:
            RConst.make_rui_cf(le)
        self.hash_arr = [1]
        for el in S:
            self.hash_arr.append(RConst.calc_mod(
                RConst.mul(self.hash_arr[-1], RConst.cf) + ord(el)))

    def get(self, l: int, r: int) -> int:
        "hash(S[l..r))"
        return RConst.sub(self.hash_arr[r],
                          RConst.mul(self.hash_arr[l], RConst.rui_cf[r - l]))


class RConst:
    Mask30 = (1 << 30) - 1
    Mask31 = (1 << 31) - 1
    Mod = (1 << 61) - 1
    cf = 0
    rui_cf = [1]

    @staticmethod
    def calc_mod(x: int) -> int:
        xu, xd = x >> 61, x & RConst.Mod
        ret = xu + xd
        if RConst.Mod <= ret:
            ret -= RConst.Mod
        return ret

    @staticmethod
    def sub(a: int, b: int) -> int:
        if a < b:
            return a + RConst.Mod - b
        return a - b

    @staticmethod
    def mul(a: int, b: int) -> int:
        au, ad = a >> 31, a & RConst.Mask31
        bu, bd = b >> 31, b & RConst.Mask31
        mid = ad * bu + au * bd
        midu, midd = mid >> 30, mid & RConst.Mask30
        return RConst.calc_mod(((au * bu) << 1) + midu + (midd << 31) + ad * bd)

    @staticmethod
    def make_rui_cf(x: int):
        l = len(RConst.rui_cf)
        for _ in range(x - l + 1):
            RConst.rui_cf.append(RConst.mul(RConst.cf, RConst.rui_cf[-1]))


def main():
    S = input()

    N = len(S)
    S_rev = S[::-1]
    rh = RollingHash(S)
    rh_rev = RollingHash(S_rev)

    def is_palindrome(l, r):
        return rh.get(l, r) == rh_rev.get(N - r, N - l)

    G = [[] for _ in range(N + 1)]
    for i in range(N):
        for j in range(i + 1, N + 1):
            if is_palindrome(i, j):
                G[i].append(j)

    score = [0] * (N + 1)
    score[0] = 1 << 30
    for i in range(N):
        before_score = score[i]
        for ni in G[i]:
            length = ni - i
            new_score = min(length, before_score)
            score[ni] = max(score[ni], new_score)
    print(score[-1])


if __name__ == "__main__":
    main()
0