結果

問題 No.2147 ハノイの塔のおもちゃ
ユーザー hotman78hotman78
提出日時 2022-12-04 10:26:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 51 ms / 2,000 ms
コード長 359 bytes
コンパイル時間 165 ms
コンパイル使用メモリ 82,444 KB
実行使用メモリ 65,408 KB
最終ジャッジ日時 2024-04-19 18:01:51
合計ジャッジ時間 1,158 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
52,480 KB
testcase_01 AC 33 ms
52,076 KB
testcase_02 AC 32 ms
51,968 KB
testcase_03 AC 34 ms
52,224 KB
testcase_04 AC 34 ms
51,968 KB
testcase_05 AC 37 ms
52,608 KB
testcase_06 AC 42 ms
60,928 KB
testcase_07 AC 48 ms
65,408 KB
testcase_08 AC 33 ms
52,096 KB
testcase_09 AC 33 ms
52,224 KB
testcase_10 AC 48 ms
65,256 KB
testcase_11 AC 51 ms
65,280 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

dp=[0,0,0]
n=int(input())
s=input()
p2=0
for i in range(n):
    dp2=[0,0,0]
    for j in range(3):
        if ord(s[i])==(ord('A')+j):
            dp2[j]=min(dp[j],dp[(j+1)%3]+p2,dp[(j+2)%3]+p2)
        else:
            dp2[j]=min(dp[j]+p2*2+1,dp[ord(s[i])-ord('A')]+p2*2+1,dp[3-j-(ord(s[i])-ord('A'))]+p2+1)
    dp=dp2
    p2=p2*2+1
print(dp[0]%1000000007)
0