목차

단어의 개수

ps
링크acmicpc.net/…
출처BOJ
문제 번호24528
문제명단어의 개수
레벨플래티넘 4
분류

DP

시간복잡도O(n)
인풋사이즈n<=1,000,000
사용한 언어Python 3.13
제출기록32412KB / 828ms
최고기록624ms
해결날짜2026/03/16

풀이

코드

"""Solution code for "BOJ 24528. 단어의 개수".

- Problem link: https://www.acmicpc.net/problem/24528
- Solution link: http://www.teferi.net/ps/problems/boj/24528

Tags: [DP]
"""

import sys

MOD = 998_244_353


def main():
    N = int(sys.stdin.readline())

    count_by_last_ch = [0] * 26
    tot_count = 1
    for _ in range(N):
        c, v = sys.stdin.readline().split()

        c_ord, v = ord(c) - 97, int(v)
        count_c = count_by_last_ch[c_ord]
        tot_count, count_by_last_ch[c_ord] = (
            (tot_count * (v + 1) - count_c * v) % MOD,
            (tot_count * v - count_c * (v - 1)) % MOD,
        )

    print((tot_count - 1) % MOD)


if __name__ == '__main__':
    main()