結果

問題 No.225 文字列変更(medium)
ユーザー tentententen
提出日時 2022-08-19 19:42:31
言語 Java21
(openjdk 21)
結果
AC  
実行時間 128 ms / 5,000 ms
コード長 1,639 bytes
コンパイル時間 2,165 ms
コンパイル使用メモリ 77,896 KB
実行使用メモリ 47,004 KB
最終ジャッジ日時 2024-04-16 22:40:00
合計ジャッジ時間 4,856 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
40,336 KB
testcase_01 AC 96 ms
41,996 KB
testcase_02 AC 54 ms
36,808 KB
testcase_03 AC 54 ms
36,936 KB
testcase_04 AC 54 ms
36,628 KB
testcase_05 AC 53 ms
36,728 KB
testcase_06 AC 54 ms
37,020 KB
testcase_07 AC 53 ms
37,060 KB
testcase_08 AC 53 ms
36,708 KB
testcase_09 AC 53 ms
36,648 KB
testcase_10 AC 54 ms
36,504 KB
testcase_11 AC 54 ms
37,068 KB
testcase_12 AC 108 ms
42,904 KB
testcase_13 AC 111 ms
46,792 KB
testcase_14 AC 128 ms
47,004 KB
testcase_15 AC 95 ms
45,960 KB
testcase_16 AC 111 ms
46,848 KB
testcase_17 AC 98 ms
42,684 KB
testcase_18 AC 103 ms
42,860 KB
testcase_19 AC 111 ms
46,860 KB
testcase_20 AC 121 ms
43,164 KB
testcase_21 AC 118 ms
43,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    static char[] s;
    static char[] t;
    static int[][] dp;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        s = sc.next().toCharArray();
        t = 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 a, int b) {
        if (a < 0) {
            return b + 1;
        }
        if (b < 0) {
            return a + 1;
        }
        if (dp[a][b] < 0) {
            if (s[a] == t[b]) {
                dp[a][b] = dfw(a - 1, b - 1);
            } else {
                dp[a][b] = Math.min(dfw(a - 1, b), Math.min(dfw(a, b - 1), dfw(a - 1, b - 1))) + 1;
            }
        }
        return dp[a][b];
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0