結果

問題 No.225 文字列変更(medium)
ユーザー neterukunneterukun
提出日時 2021-05-05 22:29:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 143 ms / 5,000 ms
コード長 840 bytes
コンパイル時間 944 ms
コンパイル使用メモリ 86,724 KB
実行使用メモリ 84,444 KB
最終ジャッジ日時 2023-10-11 16:20:57
合計ジャッジ時間 4,543 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 105 ms
79,392 KB
testcase_01 AC 122 ms
81,648 KB
testcase_02 AC 76 ms
71,148 KB
testcase_03 AC 75 ms
71,080 KB
testcase_04 AC 78 ms
71,252 KB
testcase_05 AC 75 ms
71,280 KB
testcase_06 AC 75 ms
71,064 KB
testcase_07 AC 77 ms
71,064 KB
testcase_08 AC 77 ms
71,220 KB
testcase_09 AC 76 ms
71,108 KB
testcase_10 AC 74 ms
71,156 KB
testcase_11 AC 75 ms
71,084 KB
testcase_12 AC 137 ms
83,544 KB
testcase_13 AC 143 ms
84,444 KB
testcase_14 AC 141 ms
84,196 KB
testcase_15 AC 133 ms
83,504 KB
testcase_16 AC 136 ms
84,176 KB
testcase_17 AC 139 ms
83,528 KB
testcase_18 AC 137 ms
83,384 KB
testcase_19 AC 138 ms
83,436 KB
testcase_20 AC 138 ms
83,504 KB
testcase_21 AC 136 ms
83,684 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def levenshtein_distance(s, t):
    len_s = len(s)
    len_t = len(t)
    INF = 10 ** 18
    dp = [[INF] * (len_t + 1) for _ in range(len_s + 1)]
    for i in range(len_s + 1):
        dp[i][0] = i
    for j in range(len_t + 1):
        dp[0][j] = j

    for i in range(len_s):
        for j in range(len_t):
            # 変更操作
            if s[i] == t[j]:
                dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j])
            else:
                dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + 1)
            # 削除操作
            dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j + 1] + 1)
            # 挿入操作
            dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i + 1][j] + 1)

    return dp[len_s][len_t]


n, m = map(int, input().split())
s = input()
t = input()


print(levenshtein_distance(s, t))
0