結果

問題 No.1859 ><<<
ユーザー ygd.ygd.
提出日時 2022-03-01 00:56:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 245 ms / 2,000 ms
コード長 2,057 bytes
コンパイル時間 278 ms
コンパイル使用メモリ 87,252 KB
実行使用メモリ 161,216 KB
最終ジャッジ日時 2023-09-21 09:52:14
合計ジャッジ時間 11,961 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,428 KB
testcase_01 AC 65 ms
71,356 KB
testcase_02 AC 67 ms
71,500 KB
testcase_03 AC 66 ms
71,528 KB
testcase_04 AC 245 ms
154,636 KB
testcase_05 AC 239 ms
154,492 KB
testcase_06 AC 236 ms
154,220 KB
testcase_07 AC 232 ms
154,448 KB
testcase_08 AC 240 ms
154,564 KB
testcase_09 AC 238 ms
154,468 KB
testcase_10 AC 98 ms
88,724 KB
testcase_11 AC 165 ms
106,712 KB
testcase_12 AC 153 ms
104,772 KB
testcase_13 AC 128 ms
106,428 KB
testcase_14 AC 166 ms
109,444 KB
testcase_15 AC 206 ms
142,952 KB
testcase_16 AC 224 ms
149,860 KB
testcase_17 AC 177 ms
112,684 KB
testcase_18 AC 219 ms
149,660 KB
testcase_19 AC 170 ms
109,480 KB
testcase_20 AC 204 ms
143,388 KB
testcase_21 AC 218 ms
152,944 KB
testcase_22 AC 213 ms
152,080 KB
testcase_23 AC 205 ms
142,512 KB
testcase_24 AC 214 ms
152,820 KB
testcase_25 AC 225 ms
150,024 KB
testcase_26 AC 207 ms
143,480 KB
testcase_27 AC 225 ms
149,628 KB
testcase_28 AC 212 ms
149,112 KB
testcase_29 AC 217 ms
148,644 KB
testcase_30 AC 210 ms
142,584 KB
testcase_31 AC 206 ms
142,476 KB
testcase_32 AC 226 ms
153,064 KB
testcase_33 AC 208 ms
142,496 KB
testcase_34 AC 212 ms
149,416 KB
testcase_35 AC 226 ms
152,996 KB
testcase_36 AC 223 ms
153,172 KB
testcase_37 AC 211 ms
143,388 KB
testcase_38 AC 230 ms
153,644 KB
testcase_39 AC 225 ms
153,320 KB
testcase_40 AC 231 ms
161,216 KB
testcase_41 AC 214 ms
153,208 KB
testcase_42 AC 207 ms
142,560 KB
testcase_43 AC 224 ms
140,592 KB
testcase_44 AC 227 ms
140,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
#input = sys.stdin.readline
#input = sys.stdin.buffer.readline #文字列はダメ
#sys.setrecursionlimit(1000000)
#import bisect
#import itertools
#import random
#from heapq import heapify, heappop, heappush
#from collections import defaultdict 
#from collections import deque
#import copy
#import math
#from functools import lru_cache
#@lru_cache(maxsize=None)
#MOD = pow(10,9) + 7
#MOD = 998244353
#dx = [1,0,-1,0]
#dy = [0,1,0,-1]

#一つ目の方法
class RollingHash():
    def __init__(self, s, base, mod):
        self.mod = mod
        self.pw = pw = [1]*(len(s)+1)
 
        l = len(s)
        self.h = h = [0]*(l+1)
 
        v = 0
        for i in range(l):
            h[i+1] = v = (v * base + ord(s[i])) % mod #文字ではなく数字のリストの場合はord不要
        v = 1
        for i in range(l):
            pw[i+1] = v = v * base % mod
    def get(self, l, r): #S[l:r)indexでいうl文字目からr-1文字目
        return (self.h[r] - self.h[l] * self.pw[r-l]) % self.mod

base1 = 1007; base2 = 2009
mod1 = 1000000007; mod2 = 1000000009

def main():
    N = int(input())
    A = list(map(int,input().split()))
    S = list(str(input()))
    AS = []
    for i in range(N-1):
        if A[i] < A[i+1]:
            AS.append('<')
        else:
            AS.append('>')
    #print(AS,S)
    #操作不要
    if AS == S:
        print(0);exit()
    #ループする場合
    if A[N-1] < A[0]:
        AS.append('<')
    else:
        AS.append('>')

    #print(AS)
    RHS1 = RollingHash(S,base1,mod1) #N-1文字
    RHA1 = RollingHash(AS,base1,mod1) #N文字
    RHS2 = RollingHash(S,base2,mod2) #N-1文字
    RHA2 = RollingHash(AS,base2,mod2) #N文字
    #print(RHS.get(0,N-1))
    for i in range(1,N):
        #[i,N) + [0,i-1)
        mid = N - i
        if RHS1.get(0,mid) == RHA1.get(i,N) and RHS1.get(mid,N-1) == RHA1.get(0,i-1):
            if RHS2.get(0,mid) == RHA2.get(i,N) and RHS2.get(mid,N-1) == RHA2.get(0,i-1):
                print(i);exit()
    print(-1)


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