結果

問題 No.225 文字列変更(medium)
ユーザー FGtatsuroFGtatsuro
提出日時 2021-03-14 02:17:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 109 ms / 5,000 ms
コード長 813 bytes
コンパイル時間 205 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 83,584 KB
最終ジャッジ日時 2024-04-23 14:36:01
合計ジャッジ時間 2,519 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
78,848 KB
testcase_01 AC 88 ms
80,768 KB
testcase_02 AC 35 ms
51,968 KB
testcase_03 AC 35 ms
51,968 KB
testcase_04 AC 36 ms
52,224 KB
testcase_05 AC 37 ms
52,224 KB
testcase_06 AC 38 ms
52,096 KB
testcase_07 AC 39 ms
51,968 KB
testcase_08 AC 41 ms
51,968 KB
testcase_09 AC 39 ms
52,480 KB
testcase_10 AC 37 ms
52,480 KB
testcase_11 AC 37 ms
51,968 KB
testcase_12 AC 101 ms
82,944 KB
testcase_13 AC 103 ms
83,456 KB
testcase_14 AC 101 ms
83,584 KB
testcase_15 AC 95 ms
82,944 KB
testcase_16 AC 96 ms
82,944 KB
testcase_17 AC 100 ms
83,072 KB
testcase_18 AC 109 ms
83,200 KB
testcase_19 AC 103 ms
83,072 KB
testcase_20 AC 97 ms
82,688 KB
testcase_21 AC 98 ms
83,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

n, m = map(int, input().split())
S = input().strip()
T = input().strip()

INF = float('inf')

# dp[i+1][j+1]: S[i]/T[j]までの最少コスト(S, Tは0オリジン)
dp = [[INF] * (len(T) + 1) for _ in range((len(S) + 1))]

# 初期条件
dp[0][0] = 0
# 計算量が許すならば、自明な値はfor文を別にすると考えやすい
for i in range(len(S)):
    dp[i+1][0] = dp[i][0] + 1
for j in range(len(T)):
    dp[0][j+1] = dp[0][j] + 1

# 漸化式
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][j], dp[i+1][j] + 1, dp[i][j+1] + 1)
        else:
            dp[i+1][j+1] = min(dp[i][j] + 1, dp[i+1][j] + 1, dp[i][j+1] + 1)

# 求める値
print(dp[len(S)][len(T)])
0