結果
| 問題 |
No.225 文字列変更(medium)
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2021-12-02 19:12:09 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 115 ms / 5,000 ms |
| コード長 | 1,656 bytes |
| コンパイル時間 | 2,211 ms |
| コンパイル使用メモリ | 77,948 KB |
| 実行使用メモリ | 47,004 KB |
| 最終ジャッジ日時 | 2024-07-05 02:01:01 |
| 合計ジャッジ時間 | 4,919 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 22 |
ソースコード
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();
}
}
tenten