結果

問題 No.1848 Long Prefixes
ユーザー 👑 potato167potato167
提出日時 2021-12-07 14:58:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 198 ms / 2,000 ms
コード長 758 bytes
コンパイル時間 1,413 ms
コンパイル使用メモリ 86,600 KB
実行使用メモリ 125,836 KB
最終ジャッジ日時 2023-09-11 18:01:43
合計ジャッジ時間 8,890 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 182 ms
124,360 KB
testcase_01 AC 163 ms
110,872 KB
testcase_02 AC 176 ms
124,368 KB
testcase_03 AC 179 ms
123,948 KB
testcase_04 AC 138 ms
110,708 KB
testcase_05 AC 198 ms
125,836 KB
testcase_06 AC 171 ms
122,880 KB
testcase_07 AC 136 ms
111,000 KB
testcase_08 AC 172 ms
123,392 KB
testcase_09 AC 175 ms
125,724 KB
testcase_10 AC 165 ms
110,844 KB
testcase_11 AC 180 ms
124,892 KB
testcase_12 AC 171 ms
123,024 KB
testcase_13 AC 134 ms
110,892 KB
testcase_14 AC 176 ms
124,564 KB
testcase_15 AC 84 ms
76,252 KB
testcase_16 AC 81 ms
76,376 KB
testcase_17 AC 83 ms
76,636 KB
testcase_18 AC 80 ms
76,436 KB
testcase_19 AC 80 ms
76,432 KB
testcase_20 AC 73 ms
71,188 KB
testcase_21 AC 70 ms
71,276 KB
testcase_22 AC 71 ms
71,308 KB
testcase_23 AC 72 ms
71,412 KB
testcase_24 AC 71 ms
71,264 KB
testcase_25 AC 71 ms
71,144 KB
testcase_26 AC 71 ms
71,140 KB
testcase_27 AC 72 ms
71,144 KB
testcase_28 AC 72 ms
70,992 KB
testcase_29 AC 71 ms
71,428 KB
testcase_30 AC 72 ms
70,984 KB
testcase_31 AC 71 ms
71,184 KB
testcase_32 AC 73 ms
71,204 KB
testcase_33 AC 115 ms
94,204 KB
testcase_34 AC 131 ms
102,816 KB
testcase_35 AC 94 ms
81,684 KB
testcase_36 AC 118 ms
94,128 KB
testcase_37 AC 126 ms
100,952 KB
testcase_38 AC 120 ms
97,856 KB
testcase_39 AC 112 ms
94,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def z_algo(p):
	n=len(p)
	Z=[0]*n
	Z[0]=n
	i=1
	j=0
	while i<n:
		while i+j<n and p[j]==p[i+j]:
			j+=1
		Z[i]=j
		if j==0:
			i+=1
			continue
		k=1
		while k<j and k+Z[k]<j:
			Z[i+k]=Z[k]
			k+=1
		i+=k
		j-=k
	return Z

N=int(input())
A=list(map(int,input().split()))
S=input()
M=10**9+7
half=(M+1)//2
p=[('#',100)]*(N)
cum_sum=[0]*(N+1)
for i in range(N-1):
	p[i]=(S[i+1],A[i+1])
	cum_sum[i+1]=(cum_sum[i]+A[i+1])%M
cum_sum[N]=cum_sum[N-1]
Z=z_algo(p)
ans=0
for i in range(N):
	if S[i]!=S[0]:
		continue
	if A[i]<A[0]:
		ans+=((A[i]+1)*A[i])//2
		ans%=M 
	else:
		ans+=(A[0]*(A[i]-A[0]))
		ans%=M
		ans+=(A[0]*(A[0]+1))//2
		ans%=M
		ans+=cum_sum[Z[i]]
		if i+Z[i]+1<N and S[i+1+Z[i]]==S[1+Z[i]]:
			ans+=min(A[i+1+Z[i]],A[1+Z[i]])
		ans%=M
print(ans)

0