結果

問題 No.1845 Long Substrings
ユーザー tamatotamato
提出日時 2022-02-18 23:27:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 471 ms / 2,000 ms
コード長 1,303 bytes
コンパイル時間 321 ms
コンパイル使用メモリ 87,660 KB
実行使用メモリ 149,560 KB
最終ジャッジ日時 2023-09-11 20:09:17
合計ジャッジ時間 10,147 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 367 ms
149,332 KB
testcase_01 AC 419 ms
138,804 KB
testcase_02 AC 438 ms
149,560 KB
testcase_03 AC 470 ms
149,220 KB
testcase_04 AC 471 ms
138,984 KB
testcase_05 AC 340 ms
141,524 KB
testcase_06 AC 398 ms
146,588 KB
testcase_07 AC 431 ms
138,976 KB
testcase_08 AC 443 ms
145,456 KB
testcase_09 AC 446 ms
146,496 KB
testcase_10 AC 355 ms
138,556 KB
testcase_11 AC 399 ms
141,440 KB
testcase_12 AC 433 ms
143,688 KB
testcase_13 AC 422 ms
137,040 KB
testcase_14 AC 454 ms
148,416 KB
testcase_15 AC 91 ms
76,776 KB
testcase_16 AC 89 ms
76,656 KB
testcase_17 AC 93 ms
77,060 KB
testcase_18 AC 91 ms
76,596 KB
testcase_19 AC 97 ms
78,800 KB
testcase_20 AC 70 ms
71,588 KB
testcase_21 AC 70 ms
71,636 KB
testcase_22 AC 70 ms
71,392 KB
testcase_23 AC 71 ms
71,728 KB
testcase_24 AC 70 ms
71,564 KB
testcase_25 AC 68 ms
71,560 KB
testcase_26 AC 70 ms
71,400 KB
testcase_27 AC 70 ms
71,676 KB
testcase_28 AC 68 ms
71,512 KB
testcase_29 AC 69 ms
71,564 KB
testcase_30 AC 69 ms
71,576 KB
testcase_31 AC 69 ms
71,480 KB
testcase_32 AC 70 ms
71,204 KB
testcase_33 AC 70 ms
71,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

    N = int(input())
    A = list(map(int, input().split()))
    S = input().rstrip('\n')

    nxt = [[-1] * 26 for _ in range(N + 1)]
    for i in range(N - 1, -1, -1):
        s = S[i]
        for j in range(26):
            nxt[i][j] = nxt[i + 1][j]
            if ord(s) - 97 == j:
                nxt[i][j] = i + 1

    dp0 = [0] * (N + 1)
    dp1 = [0] * (N + 1)
    dp0[0] = 1
    for i, s in enumerate(S):
        for c in range(26):
            j = nxt[i][c]
            if j == -1:
                continue
            a = A[j - 1]
            if i != 0:
                if ord(S[i - 1]) - 97 == c:
                    dp0[j] = (dp0[j] + (dp1[i] * (a - 1)) % mod) % mod
                    dp1[j] = (dp1[j] + dp1[i]) % mod
                else:
                    dp0[j] = (dp0[j] + ((dp0[i] + dp1[i]) * (a - 1)) % mod) % mod
                    dp1[j] = (dp1[j] + dp0[i] + dp1[i]) % mod
            else:
                dp0[j] = (dp0[j] + ((dp0[i] + dp1[i]) * (a - 1)) % mod)%mod
                dp1[j] = (dp1[j] + dp0[i] + dp1[i])%mod
    ans = 0
    for i in range(1, N + 1):
        ans = (ans + dp0[i])%mod
        ans = (ans + dp1[i])%mod
    print(ans)


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