結果

問題 No.1859 ><<<
ユーザー 👑 KazunKazun
提出日時 2022-03-03 19:48:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 276 ms / 2,000 ms
コード長 3,578 bytes
コンパイル時間 278 ms
コンパイル使用メモリ 87,128 KB
実行使用メモリ 159,848 KB
最終ジャッジ日時 2023-09-24 15:32:48
合計ジャッジ時間 13,920 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
72,196 KB
testcase_01 AC 87 ms
72,064 KB
testcase_02 AC 89 ms
72,088 KB
testcase_03 AC 88 ms
72,012 KB
testcase_04 AC 276 ms
159,344 KB
testcase_05 AC 253 ms
159,588 KB
testcase_06 AC 252 ms
159,844 KB
testcase_07 AC 252 ms
159,760 KB
testcase_08 AC 252 ms
159,848 KB
testcase_09 AC 254 ms
159,524 KB
testcase_10 AC 134 ms
88,428 KB
testcase_11 AC 200 ms
123,044 KB
testcase_12 AC 189 ms
117,396 KB
testcase_13 AC 157 ms
100,620 KB
testcase_14 AC 207 ms
127,816 KB
testcase_15 AC 237 ms
144,464 KB
testcase_16 AC 252 ms
151,588 KB
testcase_17 AC 219 ms
134,600 KB
testcase_18 AC 248 ms
151,152 KB
testcase_19 AC 210 ms
130,028 KB
testcase_20 AC 230 ms
144,872 KB
testcase_21 AC 252 ms
153,920 KB
testcase_22 AC 240 ms
151,952 KB
testcase_23 AC 237 ms
144,060 KB
testcase_24 AC 246 ms
152,876 KB
testcase_25 AC 254 ms
151,592 KB
testcase_26 AC 241 ms
145,052 KB
testcase_27 AC 252 ms
151,524 KB
testcase_28 AC 246 ms
150,744 KB
testcase_29 AC 247 ms
150,292 KB
testcase_30 AC 239 ms
144,256 KB
testcase_31 AC 239 ms
144,132 KB
testcase_32 AC 259 ms
153,840 KB
testcase_33 AC 242 ms
143,932 KB
testcase_34 AC 249 ms
150,752 KB
testcase_35 AC 261 ms
154,000 KB
testcase_36 AC 259 ms
153,752 KB
testcase_37 AC 240 ms
144,648 KB
testcase_38 AC 261 ms
154,300 KB
testcase_39 AC 260 ms
154,192 KB
testcase_40 AC 245 ms
152,952 KB
testcase_41 AC 243 ms
153,752 KB
testcase_42 AC 229 ms
143,644 KB
testcase_43 AC 240 ms
151,560 KB
testcase_44 AC 244 ms
151,848 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Rolling_Hash():
    def __init__(self,S,Base,Mod):
        self.Mod=Mod
        self.Base=Base
        self.string=S
        self.power=power=[1]*(len(S)+1)

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

        v=0
        for i in range(L):
            #h[i+1]=v=(v*Base+ord(S[i]))%Mod
            h[i+1]=v=(v*Base+S[i])%Mod

        v=1
        for i in range(L):
            power[i+1]=v=(v*Base)%Mod

    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=0
        for t in T:
            alpha=(alpha*self.Base+ord(t))%self.Mod

        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=0
        for t in T:
            alpha=(alpha*self.Base+ord(t))%self.Mod

        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=0
        for t in T:
            alpha=(alpha*self.Base+ord(t))%self.Mod

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

    def index(self,T,start=0):
        alpha=0
        for t in T:
            alpha=(alpha*self.Base+ord(t))%self.Mod

        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 index(self,T,start=0):
        alpha=0
        for t in T:
            alpha=(alpha*self.Base+ord(t))%self.Mod

        for i in range(len(self)-len(T),start-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+=len(self.string)
            assert 0<=index<len(self.string)
            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 len(self.string)

class Hasher():
    def __init__(self, length, base, mod):
        self.length=length
        self.base=base
        self.mod=mod

        self.power=pw=[1]*length
        for i in range(1,length):
            pw[i]=(base*pw[i-1])%mod

    def __repr__(self):
        return "length: {}\nbase: {}\nmod: {}".format(self.length, self.base, self.mod)

    def encode(self, S):
        code=0; N=len(S)
        for i in range(N):
            #code+=ord(S[i])*self.power[N-1-i]%self.mod
            code+=S[i]*self.power[N-1-i]%self.mod

        return code%self.mod

    def decode(self, S):
        pass

#==================================================
from random import randint as ri

N=int(input())
A=list(map(int,input().split()))
S=input()

Mod1=10**9+7; Mod2=10**9+9
a=ri(2,Mod1-1); b=ri(2,Mod1-1); p=ri(2,Mod1-1)

B=[a if A[i%N]<A[(i+1)%N] else b for i in range(2*N-1)]
T=[a if S[i]=="<" else  b for i in range(N-1)]

BH1=Rolling_Hash(B,p,Mod1); BH2=Rolling_Hash(B,p,Mod2)
H1=Hasher(N,p,Mod1); H2=Hasher(N,p,Mod2)
h1=H1.encode(T); h2=H2.encode(T)

Ans=-1
for i in range(N):
    if BH1[i:i+N-1]==h1 and BH2[i:i+N-1]==h2:
        Ans=i
        break

print(Ans)
0