結果

問題 No.225 文字列変更(medium)
ユーザー tentententen
提出日時 2020-12-23 09:38:38
言語 Java21
(openjdk 21)
結果
AC  
実行時間 216 ms / 5,000 ms
コード長 941 bytes
コンパイル時間 2,486 ms
コンパイル使用メモリ 77,800 KB
実行使用メモリ 62,708 KB
最終ジャッジ日時 2023-10-21 15:03:10
合計ジャッジ時間 7,505 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 195 ms
60,200 KB
testcase_01 AC 196 ms
60,032 KB
testcase_02 AC 141 ms
57,452 KB
testcase_03 AC 141 ms
57,452 KB
testcase_04 AC 139 ms
57,392 KB
testcase_05 AC 139 ms
57,428 KB
testcase_06 AC 141 ms
55,412 KB
testcase_07 AC 139 ms
57,420 KB
testcase_08 AC 147 ms
57,308 KB
testcase_09 AC 143 ms
57,372 KB
testcase_10 AC 142 ms
57,424 KB
testcase_11 AC 139 ms
57,128 KB
testcase_12 AC 205 ms
62,708 KB
testcase_13 AC 216 ms
62,580 KB
testcase_14 AC 214 ms
62,556 KB
testcase_15 AC 192 ms
62,352 KB
testcase_16 AC 211 ms
60,224 KB
testcase_17 AC 188 ms
62,028 KB
testcase_18 AC 207 ms
62,516 KB
testcase_19 AC 210 ms
60,520 KB
testcase_20 AC 216 ms
62,500 KB
testcase_21 AC 216 ms
62,512 KB
権限があれば一括ダウンロードができます

ソースコード

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