結果

問題 No.225 文字列変更(medium)
ユーザー rpy3cpprpy3cpp
提出日時 2015-06-13 14:25:16
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
実行時間 -
コード長 827 bytes
コンパイル時間 104 ms
コンパイル使用メモリ 10,784 KB
実行使用メモリ 143,544 KB
最終ジャッジ日時 2023-09-21 00:03:50
合計ジャッジ時間 16,159 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 AC 16 ms
7,888 KB
testcase_03 AC 15 ms
7,836 KB
testcase_04 AC 16 ms
7,884 KB
testcase_05 AC 16 ms
7,880 KB
testcase_06 AC 16 ms
7,844 KB
testcase_07 AC 15 ms
7,820 KB
testcase_08 AC 15 ms
7,736 KB
testcase_09 AC 16 ms
7,928 KB
testcase_10 AC 16 ms
7,720 KB
testcase_11 AC 16 ms
7,832 KB
testcase_12 AC 1,536 ms
142,856 KB
testcase_13 AC 1,998 ms
143,472 KB
testcase_14 AC 1,897 ms
143,324 KB
testcase_15 AC 701 ms
71,368 KB
testcase_16 AC 1,886 ms
143,420 KB
testcase_17 AC 720 ms
71,848 KB
testcase_18 AC 1,124 ms
87,460 KB
testcase_19 AC 1,892 ms
143,544 KB
testcase_20 AC 1,302 ms
93,716 KB
testcase_21 AC 1,751 ms
143,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

memo = dict()
def f(a, b, S, T):
    '''
    f(a, b, S, T) S[:a] を T[:b] に変更するのに必要な最低ステップ数
    '''
    global memo
    if (a, b) in memo:
        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