結果
問題 | No.422 文字列変更 (Hard) |
ユーザー |
👑 |
提出日時 | 2019-06-12 00:50:20 |
言語 | D (dmd 2.109.1) |
結果 |
AC
|
実行時間 | 677 ms / 3,000 ms |
コード長 | 2,607 bytes |
コンパイル時間 | 832 ms |
コンパイル使用メモリ | 123,324 KB |
実行使用メモリ | 154,220 KB |
最終ジャッジ日時 | 2024-06-22 01:42:23 |
合計ジャッジ時間 | 8,389 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 16 |
ソースコード
import std.conv, std.functional, std.range, std.stdio, std.string;import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons;import core.bitop;class EOFException : Throwable { this() { super("EOF"); } }string[] tokens;string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }int readInt() { return readToken.to!int; }long readLong() { return readToken.to!long; }real readReal() { return readToken.to!real; }bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1;(unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }enum INF = 10^^9;int M, N;string S, T;void main() {try {for (; ; ) {M = readInt();N = readInt();S = readToken();T = readToken();auto dp = new int[][][](M + 1, N + 1, 3);auto prev = new Tuple!(int, int, int)[][][](M + 1, N + 1, 3);foreach (i; 0 .. M + 1) foreach (j; 0 .. N + 1) {dp[i][j][] = INF;}dp[0][0][0] = 0;foreach (i; 0 .. M + 1) foreach (j; 0 .. N + 1) {foreach (a; 0 .. 3) {if (i < M && chmin(dp[i + 1][j][1], dp[i][j][a] + ((a == 1) ? 2 : 9))) {prev[i + 1][j][1] = tuple(i, j, a);}if (j < N && chmin(dp[i][j + 1][2], dp[i][j][a] + ((a == 2) ? 2 : 9))) {prev[i][j + 1][2] = tuple(i, j, a);}if (i < M && j < N && chmin(dp[i + 1][j + 1][0], dp[i][j][a] + ((S[i] == T[j]) ? 0 : 5))) {prev[i + 1][j + 1][0] = tuple(i, j, a);}}}const am = cast(int)(dp[M][N].minIndex);string ansS, ansT;for (int i = M, j = N, a = am; !(i == 0 && j == 0); ) {const ii = prev[i][j][a][0];const jj = prev[i][j][a][1];const aa = prev[i][j][a][2];ansS = ((ii == i) ? '-' : S[ii]) ~ ansS;ansT = ((jj == j) ? '-' : T[jj]) ~ ansT;i = ii;j = jj;a = aa;}writeln(dp[M][N][am]);writeln(ansS);writeln(ansT);}} catch (EOFException e) {}}