結果
| 問題 |
No.422 文字列変更 (Hard)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-09-10 17:11:40 |
| 言語 | Java (openjdk 23) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 1,571 bytes |
| コンパイル時間 | 3,743 ms |
| コンパイル使用メモリ | 79,492 KB |
| 実行使用メモリ | 102,060 KB |
| 最終ジャッジ日時 | 2024-11-17 06:49:54 |
| 合計ジャッジ時間 | 10,870 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | RE * 1 |
| other | RE * 16 |
ソースコード
package N4xx;
import java.util.*;
public class N422 {
public static void main(String[] args) {
new N422().solver();
}
int N, M;
String S, T;
int[][][] dp;
void solver() {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
S = sc.next();
T = sc.next();
dp = new int[1300][1300][3];
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[i].length; j++) {
for (int k = 0; k < dp[i][j].length; j++) {
dp[i][j][i] = -1;
}
}
}
System.out.println(dpdp(0, 0, 0));
System.out.println(RS + "\n" + RT);
}
String RS = "";
String RT = "";
int dpdp(int s, int t, int st) {
if (s == N && t == M)
return 0;
int ret = dp[s][t][st];
if (ret != -1)
return ret;
ret = 10101010;
if (s < N && t < M)
ret = Math.min(ret, dpdp(s + 1, t + 1, 0) + ((S.charAt(s) == T.charAt(t) ? 0 : 5)));
if (s < N)
ret = Math.min(ret, st == 2 ? 2 : 9 + dpdp(s + 1, t, 2));
if (t < M)
ret = Math.min(ret, st == 1 ? 2 : 9 + dpdp(s, t + 1, 1));
return dp[s][t][st] = ret;
}
void dfs(int s, int t, int st) {
if (s == N && t == M)
return;
int ret = dp[s][t][st];
if (s < N && t < M && ret == (dpdp(s + 1, t + 1, 0) + S.charAt(s) == T.charAt(t) ? 0 : 5)) {
RS += S.charAt(s);
RT += T.charAt(t);
dfs(s + 1, t + 1, 0);
} else if (s < N && ret == (dpdp(s + 1, t, 2) + st == 2 ? 2 : 9)) {
RS += S.charAt(s);
RT = "-";
dfs(s + 1, t, 2);
} else if (t < M && ret == (dpdp(s, t + 2, 1) + st == 1 ? 2 : 9)) {
RS += "-";
RT += T.charAt(t);
dfs(s, t + 1, 1);
}
}
}