結果

問題 No.225 文字列変更(medium)
ユーザー titiatitia
提出日時 2022-07-17 04:33:05
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,987 ms / 5,000 ms
コード長 866 bytes
コンパイル時間 321 ms
コンパイル使用メモリ 10,928 KB
実行使用メモリ 40,816 KB
最終ジャッジ日時 2023-09-11 15:27:17
合計ジャッジ時間 22,359 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 724 ms
19,900 KB
testcase_01 AC 1,277 ms
30,040 KB
testcase_02 AC 15 ms
8,216 KB
testcase_03 AC 14 ms
7,836 KB
testcase_04 AC 15 ms
8,252 KB
testcase_05 AC 15 ms
8,224 KB
testcase_06 AC 16 ms
8,244 KB
testcase_07 AC 15 ms
8,416 KB
testcase_08 AC 15 ms
8,340 KB
testcase_09 AC 16 ms
8,376 KB
testcase_10 AC 16 ms
7,972 KB
testcase_11 AC 15 ms
8,324 KB
testcase_12 AC 1,728 ms
36,092 KB
testcase_13 AC 1,987 ms
40,816 KB
testcase_14 AC 1,886 ms
39,368 KB
testcase_15 AC 1,748 ms
30,964 KB
testcase_16 AC 1,784 ms
38,492 KB
testcase_17 AC 1,755 ms
31,108 KB
testcase_18 AC 1,739 ms
33,336 KB
testcase_19 AC 1,818 ms
38,868 KB
testcase_20 AC 1,672 ms
34,052 KB
testcase_21 AC 1,746 ms
37,692 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

DP=[[1<<30]*m for i in range(n)]

for i in range(m):
    if S[0]==T[i]:
        DP[0][i]=i
    else:
        DP[0][i]=i+1

for i in range(n):
    if S[i]==T[0]:
        DP[i][0]=i
    else:
        DP[i][0]=i+1

for i in range(n):
    for j in range(m):
        if S[i]==T[j]:
            if i-1>=0 and j-1>=0:
                DP[i][j]=min(DP[i-1][j-1],DP[i][j])
            if i-1>=0:
                DP[i][j]=min(DP[i-1][j]+1,DP[i][j])
            if j-1>=0:
                DP[i][j]=min(DP[i][j-1]+1,DP[i][j])
        else:
            if i-1>=0 and j-1>=0:
                DP[i][j]=min(DP[i-1][j-1]+1,DP[i][j])
            if i-1>=0:
                DP[i][j]=min(DP[i-1][j]+1,DP[i][j])
            if j-1>=0:
                DP[i][j]=min(DP[i][j-1]+1,DP[i][j])
            

print(DP[n-1][m-1])    
            
0