結果

問題 No.225 文字列変更(medium)
ユーザー rpy3cpprpy3cpp
提出日時 2015-06-13 16:08:38
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 857 bytes
コンパイル時間 90 ms
コンパイル使用メモリ 10,984 KB
実行使用メモリ 38,032 KB
最終ジャッジ日時 2023-09-21 00:53:15
合計ジャッジ時間 9,591 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 AC 41 ms
15,848 KB
testcase_03 AC 41 ms
15,720 KB
testcase_04 AC 41 ms
15,764 KB
testcase_05 AC 41 ms
15,692 KB
testcase_06 AC 41 ms
15,776 KB
testcase_07 AC 41 ms
15,716 KB
testcase_08 AC 41 ms
15,720 KB
testcase_09 AC 41 ms
15,776 KB
testcase_10 AC 42 ms
15,832 KB
testcase_11 AC 42 ms
15,772 KB
testcase_12 AC 805 ms
30,104 KB
testcase_13 AC 1,082 ms
38,032 KB
testcase_14 AC 981 ms
35,696 KB
testcase_15 AC 370 ms
17,500 KB
testcase_16 AC 1,005 ms
36,068 KB
testcase_17 AC 382 ms
17,692 KB
testcase_18 AC 607 ms
23,760 KB
testcase_19 AC 1,000 ms
36,656 KB
testcase_20 AC 698 ms
26,828 KB
testcase_21 AC 930 ms
34,132 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def read_data():
    n, m = map(int, input().split())
    S = input().strip()
    T = input().strip()
    return n, m, S, T

memo = [[-1] * 1001 for i in range(1001)]
def f(a, b, S, T):
    '''
    f(a, b, S, T) S[:a] を T[:b] に変更するのに必要な最低ステップ数
    '''
    global memo
    if memo[a][b] >= 0:
        return memo[a][b]
    if a == 0:
        return b
    if b == 0:
        return a
    s = S[a - 1]
    t = T[b - 1]
    if s == t:
        memo[a][b] = f(a-1, b-1, S, T)
        return memo[a][b]
    if s != t:
        steps_change = 1 + f(a-1, b-1, S, T)
        steps_insert = 1 + f(a,   b-1, S, T)
        steps_delete = 1 + f(a-1, b,   S, T)
        memo[a][b] = min(steps_change, steps_insert, steps_delete)
        return memo[a][b]

if __name__ == '__main__':
    n, m, S, T = read_data()
    print(f(n, m, S, T))
0