結果

問題 No.225 文字列変更(medium)
ユーザー tentententen
提出日時 2021-12-02 19:12:09
言語 Java21
(openjdk 21)
結果
AC  
実行時間 101 ms / 5,000 ms
コード長 1,656 bytes
コンパイル時間 2,178 ms
コンパイル使用メモリ 74,532 KB
実行使用メモリ 57,560 KB
最終ジャッジ日時 2023-09-18 10:25:52
合計ジャッジ時間 4,456 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
52,868 KB
testcase_01 AC 88 ms
55,392 KB
testcase_02 AC 42 ms
49,172 KB
testcase_03 AC 42 ms
49,268 KB
testcase_04 AC 42 ms
49,288 KB
testcase_05 AC 41 ms
49,440 KB
testcase_06 AC 43 ms
49,608 KB
testcase_07 AC 41 ms
49,256 KB
testcase_08 AC 42 ms
49,288 KB
testcase_09 AC 42 ms
49,312 KB
testcase_10 AC 43 ms
49,228 KB
testcase_11 AC 43 ms
49,400 KB
testcase_12 AC 89 ms
57,560 KB
testcase_13 AC 101 ms
56,936 KB
testcase_14 AC 91 ms
57,472 KB
testcase_15 AC 94 ms
56,456 KB
testcase_16 AC 95 ms
56,840 KB
testcase_17 AC 83 ms
56,656 KB
testcase_18 AC 97 ms
56,820 KB
testcase_19 AC 92 ms
57,392 KB
testcase_20 AC 85 ms
55,012 KB
testcase_21 AC 96 ms
57,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static char[] start;
    static char[] term;
    static int[][] dp;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        start = sc.next().toCharArray();
        term = 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 x, int y) {
        if (x < 0) {
            return y + 1;
        }
        if (y < 0) {
            return x + 1;
        }
        if (dp[x][y] < 0) {
            if (start[x] == term[y]) {
                dp[x][y] = dfw(x - 1, y - 1);
            } else {
                dp[x][y] = Math.min(dfw(x - 1, y - 1), Math.min(dfw(x - 1, y), dfw(x, y - 1))) + 1;
            }
        }
        return dp[x][y];
    }
}
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 {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0