結果

問題 No.225 文字列変更(medium)
ユーザー mobius_bkstmobius_bkst
提出日時 2015-06-15 13:55:10
言語 Java21
(openjdk 21)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,996 bytes
コンパイル時間 4,171 ms
コンパイル使用メモリ 75,104 KB
実行使用メモリ 61,820 KB
最終ジャッジ日時 2023-09-21 02:08:58
合計ジャッジ時間 7,417 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 58 ms
54,480 KB
testcase_03 AC 54 ms
54,308 KB
testcase_04 AC 57 ms
54,684 KB
testcase_05 AC 57 ms
54,472 KB
testcase_06 AC 58 ms
54,836 KB
testcase_07 AC 58 ms
54,444 KB
testcase_08 AC 56 ms
54,656 KB
testcase_09 AC 57 ms
54,464 KB
testcase_10 AC 59 ms
54,520 KB
testcase_11 AC 58 ms
54,604 KB
testcase_12 AC 171 ms
61,252 KB
testcase_13 AC 187 ms
61,636 KB
testcase_14 AC 177 ms
61,248 KB
testcase_15 AC 181 ms
61,620 KB
testcase_16 AC 177 ms
61,468 KB
testcase_17 AC 184 ms
61,820 KB
testcase_18 AC 173 ms
61,216 KB
testcase_19 AC 181 ms
61,224 KB
testcase_20 AC 190 ms
61,676 KB
testcase_21 AC 176 ms
61,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class No225 {
    public static void main(String[] args) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    System.in));
            int[] a = strToIntArray(br.readLine());
            int n = a[0];
            int m = a[1];
            String S = br.readLine() + "_";
            String T = br.readLine() + "_";

            int[][] dp = new int[1001][1001];
            for (int i = 0; i < n + 1; i++) {
                for (int j = 0; j < m + 1; j++) {
                    dp[i][j] = Integer.MAX_VALUE;
                }
            }
            dp[0][0] = 0;

            for (int i = 0; i < n + 1; i++) {
                for (int j = 0; j < m + 1; j++) {

                    // 文字を追加する
                    if (i <= n) {
                        dp[i][j + 1] = Math.min(dp[i][j + 1], dp[i][j] + 1);
                    }

                    // 文字を削除する
                    if (j <= m) {
                        dp[i + 1][j] = Math.min(dp[i + 1][j], dp[i][j] + 1);
                    }

                    // 文字を変更する
                    if (S.substring(i, i + 1).equals(T.substring(j, j + 1))) {
                        dp[i + 1][j + 1] = Math.min(dp[i + 1][j + 1], dp[i][j]);
                    } else {
                        dp[i + 1][j + 1] = Math.min(dp[i + 1][j + 1],
                                dp[i][j] + 1);
                    }

                }
            }
            System.out.println(dp[n][m]);

        } catch (Exception e) {
            System.err.println("Error:" + e.getMessage());
        }
    }

    static int[] strToIntArray(String S) {
        String[] strArray = S.split(" ");
        int[] intArray = new int[strArray.length];
        for (int i = 0; i < strArray.length; i++) {
            intArray[i] = Integer.parseInt(strArray[i]);
        }
        return intArray;
    }
}
0