結果

問題 No.2204 Palindrome Splitting (No Rearrangement ver.)
ユーザー 👑 KazunKazun
提出日時 2023-02-03 21:53:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,102 ms / 2,000 ms
コード長 5,672 bytes
コンパイル時間 347 ms
コンパイル使用メモリ 87,124 KB
実行使用メモリ 78,208 KB
最終ジャッジ日時 2023-09-15 17:37:39
合計ジャッジ時間 31,069 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,248 KB
testcase_01 AC 75 ms
71,072 KB
testcase_02 AC 77 ms
71,236 KB
testcase_03 AC 668 ms
77,640 KB
testcase_04 AC 239 ms
77,592 KB
testcase_05 AC 187 ms
77,624 KB
testcase_06 AC 1,071 ms
77,896 KB
testcase_07 AC 965 ms
78,132 KB
testcase_08 AC 925 ms
77,820 KB
testcase_09 AC 936 ms
78,208 KB
testcase_10 AC 1,075 ms
77,976 KB
testcase_11 AC 889 ms
77,828 KB
testcase_12 AC 1,063 ms
77,664 KB
testcase_13 AC 1,070 ms
78,092 KB
testcase_14 AC 1,072 ms
77,760 KB
testcase_15 AC 998 ms
77,880 KB
testcase_16 AC 476 ms
77,732 KB
testcase_17 AC 592 ms
77,868 KB
testcase_18 AC 1,067 ms
78,056 KB
testcase_19 AC 1,067 ms
77,888 KB
testcase_20 AC 1,068 ms
77,892 KB
testcase_21 AC 1,074 ms
77,852 KB
testcase_22 AC 1,070 ms
77,896 KB
testcase_23 AC 1,071 ms
78,024 KB
testcase_24 AC 1,078 ms
77,856 KB
testcase_25 AC 1,078 ms
77,708 KB
testcase_26 AC 1,070 ms
77,860 KB
testcase_27 AC 738 ms
78,172 KB
testcase_28 AC 1,074 ms
77,880 KB
testcase_29 AC 1,070 ms
77,768 KB
testcase_30 AC 73 ms
70,900 KB
testcase_31 AC 74 ms
71,324 KB
testcase_32 AC 1,096 ms
77,392 KB
testcase_33 AC 1,074 ms
78,156 KB
testcase_34 AC 74 ms
71,412 KB
testcase_35 AC 1,102 ms
77,744 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Rolling_Hash():
    def __init__(self,S, base, mod, type=0):
        """ type=0: 整数列 (各要素は mod 未満), type=1: 文字列 (mod>(最大の文字コード))

        """

        self.mod=mod
        self.base=base
        self.length=len(S)
        self.power=power=[1]*(len(S)+1)
        self.type=type

        L=len(S)
        self.hash=h=[0]*(L+1)

        for i in range(L):
            if type:
                h[i+1]=(base*h[i]+ord(S[i]))%mod
            else:
                h[i+1]=(base*h[i]+S[i])%mod

        for i in range(L):
            power[i+1]=base*power[i]%mod

    def __hasher(self, X):
        assert len(X)<=len(self)
        h=0
        for i in range(len(X)):
            h=(h*self.base+X[i])%self.mod
        return h

    def get(self, l, r):
        return (self.hash[r]-self.hash[l]*self.power[r-l])%self.mod

    def count(self, T, start=0):
        alpha=self.__hasher(T)

        K=0
        for i in range(start, len(self)-len(T)+1):
            if alpha==self[i: i+len(T)]:
                K+=1
        return K

    def find(self, T, start=0):
        alpha=self.__hasher(T)

        for i in range(start, len(self)-len(T)+1):
            if alpha==self[i: i+len(T)]:
                return i
        return -1

    def rfind(self, T, start=0):
        alpha=self.__hasher(T)

        for i in range(len(self)-len(T), start-1, -1):
            if alpha==self[i: i+len(T)]:
                return i
        return -1

    def index(self, T, start=0):
        alpha=self.__hasher(T)

        for i in range(start, len(self)-len(T)+1):
            if alpha==self[i: i+len(T)]:
                return i
        raise ValueError("substring not found")

    def __getitem__(self, index):
        if index.__class__==int:
            if index<0:
                index+=self.length
            assert 0<=index<self.length
            return self.get(index, index+1)
        elif index.__class__==slice:
            assert (index.step==None) or (index.step==1)
            L=index.start if index.start else 0
            R=index.stop if index.stop else len(self)
            if L<0:
                L+=len(self)
            if R<0:
                R+=len(self)
            return self.get(L,R)

    def __len__(self):
        return self.length

    def docking(self, l0, r0, l1, r1):
        """ [l0, r0) と [l1, r1) の部分列をドッキングしたハッシュを返す.
        """

        h0=self.get(l0,r0); h1=self.get(l1,r1)
        return (h0*self.power[r1-l1]+h1)%self.mod

#=================================================
class Double_Rolling_Hash():
    def __init__(self, S, base, mod0, mod1, type):
        self.__length=len(S)
        self.__base=base
        self.__mod0=mod0
        self.__mod1=mod1
        self.__type=type

        self.rh0=Rolling_Hash(S, base, mod0, type)
        self.rh1=Rolling_Hash(S, base, mod1, type)

    def encode(self, a0, a1):
        return a0*self.__mod1+a1

    def get(self, l, r):
        a0=self.rh0.get(l,r)
        a1=self.rh1.get(l,r)
        return self.encode(a0,a1)

    def __hasher(self, X):
        assert len(X)<=len(self)
        a0=0; a1=0
        for x in X:
            if self.__type==0:
                a0=(a0*self.__base+x)%self.__mod0
                a1=(a1*self.__base+x)%self.__mod1
        return self.encode(a0,a1)

    def __getitem__(self, index):
        if index.__class__==int:
            if index<0:
                index+=self.__length
            assert 0<=index<self.__length
            return self.encode(self.rh0[index], self.rh1[index])
        elif index.__class__==slice:
            assert (index.step==None) or (index.step==1)
            L=index.start if index.start else 0
            R=index.stop if index.stop else len(self)
            if L<0:
                L+=len(self)
            if R<0:
                R+=len(self)
            return self.encode(self.rh0[L: R], self.rh1[L: R])

    def count(self, T, start=0):
        alpha=self.__hasher(T)
        K=0
        T_len=len(T)
        for i in range(start, len(self)-len(T)+1):
            if alpha==self[i: i+T_len]:
                K+=1
        return K

    def find(self, T, start=0):
        alpha=self.__hasher(T)

        for i in range(start, len(self)-len(T)+1):
            if alpha==self[i: i+len(T)]:
                return i
        return -1

    def rfind(self, T, start=0):
        alpha=self.__hasher(T)

        for i in range(len(self)-len(T), start-1, -1):
            if alpha==self[i: i+len(T)]:
                return i
        return -1

    def index(self, T, start=0):
        alpha=self.__hasher(T)

        for i in range(start, len(self)-len(T)+1):
            if alpha==self[i: i+len(T)]:
                return i
        raise ValueError("substring not found")

    def __len__(self):
        return self.__length

    def docking(self, l0, r0, l1, r1):
        """ ranges: tuple (l,r) からなるリスト, i 番目の (l,r) は部分列 [l,r) を意味する.
        """

        return self.encode(self.rh0.docking(l0, r0, l1, r1), self.rh1.docking(l0, r0, l1, r1))

#==================================================
def solve():
    S=input(); N=len(S)
    Mod0=10**9+7; Mod1=10**9+9

    SHo=Double_Rolling_Hash("*"+S, 100, Mod0, Mod1, 1)
    SHr=Double_Rolling_Hash("*"+S[::-1], 100, Mod0, Mod1, 1)

    inf=float("inf")
    DP=[-inf]*(N+1); DP[0]=inf
    for i in range(1,N+1):
        for j in range(1,i+1):
            length=i-j+1
            if SHo[j:i+1]==SHr[N-i+1:N-j+1+1]:
                DP[i]=max(DP[i], min(length, DP[j-1]))
    return DP[N]

#==================================================
print(solve())
0