結果

問題 No.225 文字列変更(medium)
ユーザー lie_of_lillielie_of_lillie
提出日時 2021-06-14 16:17:47
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 1,148 ms / 5,000 ms
コード長 857 bytes
コンパイル時間 558 ms
コンパイル使用メモリ 10,836 KB
実行使用メモリ 39,984 KB
最終ジャッジ日時 2023-08-26 01:15:15
合計ジャッジ時間 14,144 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 432 ms
19,628 KB
testcase_01 AC 765 ms
29,876 KB
testcase_02 AC 16 ms
7,948 KB
testcase_03 AC 16 ms
7,804 KB
testcase_04 AC 16 ms
7,844 KB
testcase_05 AC 15 ms
7,868 KB
testcase_06 AC 17 ms
7,932 KB
testcase_07 AC 16 ms
7,800 KB
testcase_08 AC 16 ms
7,844 KB
testcase_09 AC 16 ms
7,848 KB
testcase_10 AC 17 ms
7,800 KB
testcase_11 AC 16 ms
7,820 KB
testcase_12 AC 1,050 ms
33,268 KB
testcase_13 AC 1,148 ms
39,984 KB
testcase_14 AC 1,127 ms
38,124 KB
testcase_15 AC 1,065 ms
23,448 KB
testcase_16 AC 1,082 ms
37,400 KB
testcase_17 AC 1,072 ms
23,532 KB
testcase_18 AC 1,037 ms
28,504 KB
testcase_19 AC 1,072 ms
37,884 KB
testcase_20 AC 1,017 ms
30,744 KB
testcase_21 AC 1,073 ms
36,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def levenshtein_distance(s1, s2):
    n1, n2 = len(s1), len(s2)
    dp = [[float("inf")]*(n2+1) for _ in range(n1+1)]
    for i in range(n1 + 1):
        dp[i][0] = i
    for j in range(n2 + 1):
        dp[0][j] = j
    for i in range(n1):
        for j in range(n2):
            # 変更操作
            if s1[i] == s2[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[n1][n2], dp)

def solve():
    s1 = input()
    s2 = input()
    opnum, dp = (levenshtein_distance(s1, s2))
    print(opnum)

N,M=map(int,input().split())
solve()
0