結果

問題 No.225 文字列変更(medium)
ユーザー t8m8⛄️t8m8⛄️
提出日時 2015-06-23 17:10:33
言語 Java21
(openjdk 21)
結果
AC  
実行時間 169 ms / 5,000 ms
コード長 1,380 bytes
コンパイル時間 3,514 ms
コンパイル使用メモリ 76,576 KB
実行使用メモリ 60,516 KB
最終ジャッジ日時 2023-08-25 23:01:56
合計ジャッジ時間 7,962 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 157 ms
58,060 KB
testcase_01 AC 159 ms
59,740 KB
testcase_02 AC 126 ms
55,512 KB
testcase_03 AC 126 ms
55,700 KB
testcase_04 AC 127 ms
55,812 KB
testcase_05 AC 126 ms
55,696 KB
testcase_06 AC 127 ms
55,424 KB
testcase_07 AC 125 ms
55,876 KB
testcase_08 AC 125 ms
55,800 KB
testcase_09 AC 126 ms
56,024 KB
testcase_10 AC 127 ms
55,872 KB
testcase_11 AC 129 ms
55,904 KB
testcase_12 AC 166 ms
59,724 KB
testcase_13 AC 166 ms
60,516 KB
testcase_14 AC 165 ms
60,404 KB
testcase_15 AC 168 ms
60,396 KB
testcase_16 AC 165 ms
60,084 KB
testcase_17 AC 166 ms
60,004 KB
testcase_18 AC 169 ms
60,372 KB
testcase_19 AC 167 ms
60,128 KB
testcase_20 AC 167 ms
59,932 KB
testcase_21 AC 168 ms
60,468 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;
import static java.util.Arrays.*;
import static java.lang.Math.*;

public class No0225 {
    
    static final Scanner in = new Scanner(System.in);
    static final PrintWriter out = new PrintWriter(System.out,false);

    static void solve() {
        int n = in.nextInt();
        int m = in.nextInt();
        String s = in.next();
        String t = in.next();
        out.println(levenshteinDistance(s,t));
    }

    static int levenshteinDistance(String s, String t) {
    	int n = s.length();
    	int m = t.length();
    	int[][] dp = new int[n+1][m+1];
    	for (int i=0; i<=n; i++) {
    		for (int j=0; j<=m; j++) {
    			if (i == 0 && j == 0) continue;
    			dp[i][j] = Integer.MAX_VALUE/2;
    			if (i > 0) dp[i][j] = Math.min(dp[i][j], dp[i-1][j] + 1);
    			if (j > 0) dp[i][j] = Math.min(dp[i][j], dp[i][j-1] + 1);
    			if (i > 0 && j > 0) dp[i][j] = Math.min(dp[i][j], dp[i-1][j-1] + (s.charAt(i-1) == t.charAt(j-1) ? 0 : 1));
    		}
    	}
    	return dp[n][m];
    }

    public static void main(String[] args) {
        long start = System.currentTimeMillis();

        solve();
        out.flush();

        long end = System.currentTimeMillis();
        //trace(end-start + "ms");
        in.close();
        out.close();
    }

    static void trace(Object... o) { System.out.println(Arrays.deepToString(o));}
}
0