ABC215 E - Chain Contestant
https://atcoder.jp/contests/abc215/tasks/abc215_e (opens in a new tab)
ๆฐด่ฒไธไฝใbitDPใ
DP ใใผใใซใฏdp[i][j][k] := s[:i]ใพใงใงใๅฉ็จใใๆๅญ้ๅใjใๆๅพใฎๆๅญใkใฎๅ ดๅใฎๆฐ
้ท็งปๆนๆณใจๅๆๅใซใใคใณใใใใใ
ใพใๅๆๅใซ้ขใใฆใใใคใใฎใใใซdp[0][0][0]=1
ใจใใใจๆๅณใๅคใใฃใฆใใใฎใงใใใใงใฏ 11 ็ช็ฎใฎใขใซใใกใใใใ็จๆใใใใฎ็ถๆ
ใซๅฏพใ 1 ใไธใใใใคใพใใDP ใใผใใซใฎk
ใฎๆทปๅญๅใไธใคๅขใใใ้ท็งปๆใใใใใใฎ้ท็งปใใใใใใซ๏ผใคใพใๆใๅ
ๅดใฎใซใผใใใใ 1 ๅๅๅขใใ๏ผๅฆ็ใใใฐใใใ
ใพใ้ท็งปใๆทปๅญใซ้ๅใๅใใฎใงๅฐใๆ ฃใใชใใใๅ็ถๆ ใง่ใใใฎใฏใใณใณใในใใซๅๅ ใใใๅฆใใใงใใใใพใๅ่ ใฏใใใใพใงใซๅๅ ใใฆใใชใ or ็ดๅใซๅๅ ใใฆใใใใฎใใใใใใ้ท็งปใใใใๅพ่ ใซ้ขใใฆใฏ็ถๆ ใซใใใใใ้ท็งปใใใใ
ใใฃใฆใใใใ้ฉๅใซๅฎ่ฃ ใใใฐใใใ
MOD = 998244353
MAX_S = 10
n = int(input())
s = list(map(lambda x: ord(x) - ord('A'), input()))
# dp[i][j][k] := s[:i]ใพใงใงใๅฉ็จใใๆๅญ้ๅใjใๆๅพใฎๆๅญใkใฎๅ ดๅใฎๆฐ
dp = [[[0] * (MAX_S + 1) for _ in range(1 << MAX_S)] for _ in range(n + 1)]
dp[0][0][10] = 1
for i in range(n):
for j in range(1 << MAX_S):
for k in range(MAX_S + 1):
if True: # s[i]ใ้ธๆใใชใ
dp[i + 1][j][k] += dp[i][j][k]
dp[i + 1][j][k] %= MOD
if not 1 << s[i] & j: # s[i]ใๅ้ธๆ
dp[i + 1][j | 1 << s[i]][s[i]] += dp[i][j][k]
dp[i + 1][j | 1 << s[i]][s[i]] %= MOD
if 1 << s[i] & j and s[i] == k: # s[i]ใ้ฃ็ถใง้ธๆ
dp[i + 1][j][s[i]] += dp[i][j][k]
dp[i + 1][j][s[i]] %= MOD
# print(*dp, sep='\n')
ans = 0
for j in range(1 << MAX_S):
for k in range(MAX_S):
ans += dp[n][j][k]
ans %= MOD
print(ans)