結果

問題 No.225 文字列変更(medium)
ユーザー takeya_okinotakeya_okino
提出日時 2017-07-07 19:30:52
言語 Java21
(openjdk 21)
結果
AC  
実行時間 173 ms / 5,000 ms
コード長 969 bytes
コンパイル時間 2,155 ms
コンパイル使用メモリ 77,336 KB
実行使用メモリ 48,148 KB
最終ジャッジ日時 2024-04-16 01:16:31
合計ジャッジ時間 6,420 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 161 ms
43,008 KB
testcase_01 AC 160 ms
43,864 KB
testcase_02 AC 134 ms
41,112 KB
testcase_03 AC 131 ms
41,140 KB
testcase_04 AC 134 ms
40,940 KB
testcase_05 AC 133 ms
41,136 KB
testcase_06 AC 119 ms
40,392 KB
testcase_07 AC 133 ms
41,212 KB
testcase_08 AC 134 ms
41,180 KB
testcase_09 AC 134 ms
41,100 KB
testcase_10 AC 135 ms
41,336 KB
testcase_11 AC 134 ms
41,176 KB
testcase_12 AC 154 ms
47,012 KB
testcase_13 AC 153 ms
47,396 KB
testcase_14 AC 169 ms
47,984 KB
testcase_15 AC 171 ms
48,096 KB
testcase_16 AC 173 ms
48,132 KB
testcase_17 AC 169 ms
48,092 KB
testcase_18 AC 167 ms
47,920 KB
testcase_19 AC 172 ms
48,032 KB
testcase_20 AC 171 ms
47,764 KB
testcase_21 AC 169 ms
48,148 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
  // レーベンシュタイン距離(編集距離)を求めるプログラム
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int m = sc.nextInt();
    String s = sc.next();
    String t = sc.next();
    // dp[i][j]はsのi文字目までを編集してtのj文字目までにする編集回数の最小値
    int[][] dp = new int[n + 1][m + 1];
    for(int i = 0; i < n + 1; i++) {
      dp[i][0] = i;
    }
    for(int i = 0; i < m + 1; i++) {
      dp[0][i] = i;
    }
    for(int i = 1; i < n + 1; i++) {
      for(int j = 1; j < m + 1; j++) {
        int a = Math.min(dp[i - 1][j] + 1, dp[i][j - 1] + 1);
        if(s.charAt(i - 1) == t.charAt(j - 1)) {
          a = Math.min(a, dp[i - 1][j - 1]);
        } else {
          a = Math.min(a, dp[i - 1][j - 1] + 1);
        }
        dp[i][j] = a;
      }
    }
    System.out.println(dp[n][m]);
  }
}
0