結果

問題 No.1848 Long Prefixes
ユーザー chineristACchineristAC
提出日時 2022-02-18 23:20:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 233 ms / 2,000 ms
コード長 1,664 bytes
コンパイル時間 275 ms
コンパイル使用メモリ 87,008 KB
実行使用メモリ 149,336 KB
最終ジャッジ日時 2023-09-11 20:08:07
合計ジャッジ時間 8,725 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 229 ms
147,876 KB
testcase_01 AC 207 ms
139,788 KB
testcase_02 AC 224 ms
145,556 KB
testcase_03 AC 222 ms
149,336 KB
testcase_04 AC 209 ms
128,464 KB
testcase_05 AC 230 ms
140,692 KB
testcase_06 AC 210 ms
140,464 KB
testcase_07 AC 204 ms
119,600 KB
testcase_08 AC 222 ms
137,000 KB
testcase_09 AC 216 ms
147,952 KB
testcase_10 AC 213 ms
119,868 KB
testcase_11 AC 232 ms
143,108 KB
testcase_12 AC 210 ms
139,992 KB
testcase_13 AC 201 ms
116,864 KB
testcase_14 AC 233 ms
142,540 KB
testcase_15 AC 136 ms
81,528 KB
testcase_16 AC 126 ms
79,408 KB
testcase_17 AC 140 ms
81,420 KB
testcase_18 AC 121 ms
79,544 KB
testcase_19 AC 122 ms
79,272 KB
testcase_20 AC 110 ms
74,420 KB
testcase_21 AC 110 ms
74,324 KB
testcase_22 AC 112 ms
74,200 KB
testcase_23 AC 114 ms
74,156 KB
testcase_24 AC 112 ms
74,540 KB
testcase_25 AC 108 ms
74,196 KB
testcase_26 AC 108 ms
74,364 KB
testcase_27 AC 108 ms
74,212 KB
testcase_28 AC 109 ms
74,488 KB
testcase_29 AC 109 ms
74,204 KB
testcase_30 AC 110 ms
74,576 KB
testcase_31 AC 109 ms
74,156 KB
testcase_32 AC 111 ms
74,464 KB
testcase_33 AC 157 ms
95,312 KB
testcase_34 AC 177 ms
107,792 KB
testcase_35 AC 147 ms
88,856 KB
testcase_36 AC 161 ms
94,592 KB
testcase_37 AC 175 ms
104,340 KB
testcase_38 AC 172 ms
100,064 KB
testcase_39 AC 154 ms
95,216 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys,random,bisect
from collections import deque,defaultdict
from heapq import heapify,heappop,heappush
from itertools import permutations
from math import log,gcd

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

mod = 10**9 + 7

def Z_algorithm(s):
    N = len(s)
    Z_alg = [0]*N

    Z_alg[0] = N
    i = 1
    j = 0
    while i < N:
        while i+j < N and s[j] == s[i+j]:
            j += 1
        Z_alg[i] = j
        if j == 0:
            i += 1
            continue
        k = 1
        while i+k < N and k + Z_alg[k]<j:
            Z_alg[i+k] = Z_alg[k]
            k += 1
        i += k
        j -= k
    return Z_alg

N = int(input())
A = li()
S = input()
S = [ord(s)-ord("a") for s in S]

B = [A[0]]
T = [S[0]]
for i in range(1,N):
    if T[-1]==S[i]:
        B[-1] += A[i]
    else:
        B.append(A[i])
        T.append(S[i])

n = len(B)
if n==1:
    exit(print((B[0]*(B[0]+1)//2) % mod))
ZB = Z_algorithm(B[1:])
ZT = Z_algorithm(T[1:])

imos = [B[i] for i in range(n)]
for i in range(1,n):
    imos[i] += imos[i-1]

res = 0
for i in range(n):
    if T[i]!=T[0]:
        continue
    
    if B[i] < B[0]:
        res += B[i] * (B[i]+1)//2
        continue
    
    res += B[0] * (B[0]-1)//2 + B[0] * (B[i]-B[0])
    if i==n-1:
        res += B[0]
        continue

    k_b,k_t = ZB[i],ZT[i]
    k = min(k_b,k_t)
    if i+k==n-1:
        res += B[0] + imos[n-1] - imos[i]
    else:
        if T[k+1]!=T[i+k+1]:
            res += B[0] + imos[i+k] - imos[i]
        else:
            res += B[0] + imos[i+k] - imos[i] + min(B[k+1],B[i+k+1])
    
print(res % mod)



        

0