結果

問題 No.2147 ハノイの塔のおもちゃ
ユーザー 👑 rin204rin204
提出日時 2022-12-04 01:44:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 65 ms / 2,000 ms
コード長 856 bytes
コンパイル時間 744 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 75,264 KB
最終ジャッジ日時 2024-04-19 11:45:25
合計ジャッジ時間 1,187 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
52,096 KB
testcase_01 AC 31 ms
52,224 KB
testcase_02 AC 37 ms
52,224 KB
testcase_03 AC 37 ms
52,352 KB
testcase_04 AC 37 ms
52,736 KB
testcase_05 AC 38 ms
60,928 KB
testcase_06 AC 55 ms
67,840 KB
testcase_07 AC 63 ms
73,856 KB
testcase_08 AC 33 ms
51,840 KB
testcase_09 AC 31 ms
51,712 KB
testcase_10 AC 65 ms
75,264 KB
testcase_11 AC 60 ms
71,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10 ** 9 + 7

n = int(input())
C = [0]
for _ in range(n):
    C.append(C[-1] * 2 + 1)

S = input().rstrip("A")
if not S:
    print(0)
    exit()

if len(S) == 1:
    print(1)
    exit()

dp = [1] * 3
s = "ABC".index(S[0])
dp[s] = 0

for x, s in enumerate(S[1:], 1):
    s = "ABC".index(s)
    ndp = [float("inf")] * 3
    for i in range(3):
        for j in range(3):
            if j == s:
                if i == j:
                    ndp[i] = min(ndp[i], dp[j])
                else:
                    ndp[i] = min(ndp[i], dp[j] + C[x + 1])
            else:
                if i == j:
                    ndp[i] = min(ndp[i], dp[j] + 2 * C[x] + 1)
                elif i == s:
                    ndp[i] = min(ndp[i], dp[j] + C[x])
                else:
                    ndp[i] = min(ndp[i], dp[j] + C[x] + 1)
    dp = ndp
print(dp[0] % MOD)
0