結果

問題 No.422 文字列変更 (Hard)
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-06-12 00:50:20
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 665 ms / 3,000 ms
コード長 2,607 bytes
コンパイル時間 800 ms
コンパイル使用メモリ 111,592 KB
実行使用メモリ 154,004 KB
最終ジャッジ日時 2023-09-04 01:41:05
合計ジャッジ時間 8,865 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 418 ms
101,608 KB
testcase_02 AC 505 ms
117,316 KB
testcase_03 AC 386 ms
96,968 KB
testcase_04 AC 413 ms
100,476 KB
testcase_05 AC 525 ms
121,416 KB
testcase_06 AC 665 ms
154,004 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 6 ms
5,152 KB
testcase_09 AC 593 ms
137,696 KB
testcase_10 AC 627 ms
144,424 KB
testcase_11 AC 464 ms
103,004 KB
testcase_12 AC 377 ms
96,164 KB
testcase_13 AC 394 ms
98,572 KB
testcase_14 AC 408 ms
101,572 KB
testcase_15 AC 409 ms
101,176 KB
testcase_16 AC 461 ms
102,672 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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) {
  }
}
0