結果

問題 No.225 文字列変更(medium)
ユーザー tenten
提出日時 2020-12-23 09:38:38
言語 Java
(openjdk 23)
結果
AC  
実行時間 181 ms / 5,000 ms
コード長 941 bytes
コンパイル時間 1,942 ms
コンパイル使用メモリ 77,796 KB
実行使用メモリ 48,488 KB
最終ジャッジ日時 2024-09-21 16:17:24
合計ジャッジ時間 6,127 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static char[] sArr;
    static char[] tArr;
    static int[][] dp;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        sArr = sc.next().toCharArray();
        tArr = sc.next().toCharArray();
        dp = new int[n][m];
        for (int[] arr : dp) {
            Arrays.fill(arr, -1);
        }
        System.out.println(dfw(n - 1, m - 1));
    }
    
    static int dfw(int s, int t) {
        if (s < 0) {
            return t + 1;
        }
        if (t < 0) {
            return s + 1;
        }
        if (dp[s][t] < 0) {
            if (sArr[s] == tArr[t]) {
                dp[s][t] = dfw(s - 1, t - 1);
            } else {
                dp[s][t] = Math.min(Math.min(dfw(s - 1, t), dfw(s - 1, t - 1)), dfw(s, t - 1)) + 1;
            }
        }
        return dp[s][t];
    }
}
0