結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,452 KB
testcase_01 AC 65 ms
71,308 KB
testcase_02 AC 65 ms
71,096 KB
testcase_03 AC 65 ms
71,496 KB
testcase_04 AC 237 ms
154,304 KB
testcase_05 AC 234 ms
154,360 KB
testcase_06 AC 239 ms
154,320 KB
testcase_07 AC 236 ms
154,372 KB
testcase_08 AC 239 ms
154,268 KB
testcase_09 AC 239 ms
154,312 KB
testcase_10 AC 99 ms
88,376 KB
testcase_11 AC 163 ms
106,968 KB
testcase_12 AC 152 ms
104,576 KB
testcase_13 AC 128 ms
106,524 KB
testcase_14 AC 169 ms
109,292 KB
testcase_15 AC 207 ms
142,824 KB
testcase_16 AC 224 ms
149,856 KB
testcase_17 AC 178 ms
112,544 KB
testcase_18 AC 220 ms
149,424 KB
testcase_19 AC 175 ms
109,192 KB
testcase_20 AC 205 ms
143,340 KB
testcase_21 AC 218 ms
152,932 KB
testcase_22 AC 212 ms
152,036 KB
testcase_23 AC 203 ms
142,436 KB
testcase_24 AC 218 ms
152,656 KB
testcase_25 AC 220 ms
149,520 KB
testcase_26 AC 204 ms
143,524 KB
testcase_27 AC 223 ms
149,488 KB
testcase_28 AC 217 ms
149,152 KB
testcase_29 AC 220 ms
148,820 KB
testcase_30 AC 211 ms
142,712 KB
testcase_31 AC 207 ms
142,300 KB
testcase_32 AC 229 ms
153,044 KB
testcase_33 AC 209 ms
142,616 KB
testcase_34 AC 214 ms
149,384 KB
testcase_35 AC 226 ms
153,192 KB
testcase_36 AC 223 ms
152,872 KB
testcase_37 AC 211 ms
143,224 KB
testcase_38 AC 229 ms
153,440 KB
testcase_39 AC 224 ms
153,108 KB
testcase_40 AC 231 ms
161,680 KB
testcase_41 AC 218 ms
152,784 KB
testcase_42 AC 210 ms
142,408 KB
testcase_43 AC 222 ms
140,520 KB
testcase_44 AC 227 ms
140,756 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,base1,mod1) #N-1文字
    RHA2 = RollingHash(AS,base1,mod1) #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