結果

問題 No.1845 Long Substrings
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2022-02-18 22:13:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 482 ms / 2,000 ms
コード長 1,520 bytes
コンパイル時間 284 ms
コンパイル使用メモリ 87,096 KB
実行使用メモリ 171,948 KB
最終ジャッジ日時 2023-09-11 19:19:17
合計ジャッジ時間 10,058 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 339 ms
171,692 KB
testcase_01 AC 391 ms
162,136 KB
testcase_02 AC 433 ms
171,948 KB
testcase_03 AC 449 ms
171,700 KB
testcase_04 AC 440 ms
161,764 KB
testcase_05 AC 306 ms
163,424 KB
testcase_06 AC 378 ms
164,720 KB
testcase_07 AC 434 ms
162,012 KB
testcase_08 AC 432 ms
164,800 KB
testcase_09 AC 482 ms
168,788 KB
testcase_10 AC 329 ms
161,824 KB
testcase_11 AC 397 ms
163,516 KB
testcase_12 AC 426 ms
163,364 KB
testcase_13 AC 423 ms
162,064 KB
testcase_14 AC 441 ms
170,368 KB
testcase_15 AC 95 ms
76,700 KB
testcase_16 AC 94 ms
76,968 KB
testcase_17 AC 94 ms
77,012 KB
testcase_18 AC 102 ms
77,468 KB
testcase_19 AC 100 ms
76,904 KB
testcase_20 AC 74 ms
71,264 KB
testcase_21 AC 74 ms
71,316 KB
testcase_22 AC 76 ms
71,092 KB
testcase_23 AC 77 ms
71,448 KB
testcase_24 AC 76 ms
71,412 KB
testcase_25 AC 73 ms
71,276 KB
testcase_26 AC 77 ms
71,364 KB
testcase_27 AC 75 ms
71,312 KB
testcase_28 AC 76 ms
71,228 KB
testcase_29 AC 75 ms
71,224 KB
testcase_30 AC 75 ms
71,352 KB
testcase_31 AC 74 ms
71,408 KB
testcase_32 AC 76 ms
71,200 KB
testcase_33 AC 74 ms
71,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""

部分列dpを思い出せ…

dp[i][flag]
= i番目の区間の文字を1つ以上使用しており、
 最後の文字を使っているかどうかのflagが flagの時の場合の数

とすれば解ける
flagが Falseなら同じ文字の区間には飛べない

"""

import sys
from sys import stdin

mod = 10**9+7

N = int(stdin.readline())

A = list(map(int,stdin.readline().split()))
S = list(stdin.readline()[:-1])

A.append(1)
S.append("{")

lastinds = [None] * 27
indlis = []

for i in range(N,-1,-1):

    indlis.append( tuple(lastinds) )
    lastinds[ ord(S[i]) - ord("a") ] = i

indlis.reverse()
#print (indlis)

dp = [[0,0] for i in range(N+1)]
startend = [None] * 27

for i in range(N):

    #自分スタートの処理
    if startend[ord(S[i]) - ord("a")] == None:
        startend[ord(S[i]) - ord("a")] = 1
        dp[i][0] += A[i]-1
        dp[i][1] += 1

    #推移
    for k in range(27):
        nexind = indlis[i][k]
        if nexind == None:
            continue

        if k != ord(S[i])-ord('a'):
            dp[nexind][0] += dp[i][0] * (A[nexind]-1)
            dp[nexind][1] += dp[i][0]
            dp[nexind][0] += dp[i][1] * (A[nexind]-1)
            dp[nexind][1] += dp[i][1]
        else:
            #dp[nexind][0] += dp[i][0] * (A[nexind]-1)
            #dp[nexind][1] += dp[i][0]
            dp[nexind][0] += dp[i][1] * (A[nexind]-1)
            dp[nexind][1] += dp[i][1]

        dp[nexind][0] %= mod
        dp[nexind][1] %= mod

#print (dp)
print (dp[-1][1] % mod)
0