結果

問題 No.422 文字列変更 (Hard)
ユーザー ei1333333ei1333333
提出日時 2016-09-09 23:54:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 31 ms / 3,000 ms
コード長 2,537 bytes
コンパイル時間 1,147 ms
コンパイル使用メモリ 151,452 KB
実行使用メモリ 20,476 KB
最終ジャッジ日時 2023-08-10 21:02:30
合計ジャッジ時間 2,637 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
20,388 KB
testcase_01 AC 29 ms
20,344 KB
testcase_02 AC 30 ms
20,256 KB
testcase_03 AC 28 ms
20,348 KB
testcase_04 AC 29 ms
20,344 KB
testcase_05 AC 31 ms
20,468 KB
testcase_06 AC 22 ms
20,468 KB
testcase_07 AC 8 ms
20,464 KB
testcase_08 AC 7 ms
20,280 KB
testcase_09 AC 24 ms
20,232 KB
testcase_10 AC 28 ms
20,280 KB
testcase_11 AC 30 ms
20,476 KB
testcase_12 AC 28 ms
20,260 KB
testcase_13 AC 30 ms
20,404 KB
testcase_14 AC 29 ms
20,284 KB
testcase_15 AC 29 ms
20,288 KB
testcase_16 AC 29 ms
20,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

const int INF = 1 << 30;

int main()
{
  int dp[1201][1201];
  pair< int, int > pos[1201][1201];
  fill_n(*dp, 1201 * 1201, INF);
  fill_n(*pos, 1201 * 1201, make_pair(-1, -1));
  int poyo[1201] = {};
  int piyo[1201] = {};

  int n, m;
  string S, T;
  cin >> n >> m;
  cin >> S;
  cin >> T;

  for(int i = 0; i <= T.size(); i++) {
    dp[0][i] = (i == 0 ? 0 : 9 + 2 * (i - 1));
    if(i != 0) pos[0][i] = {0, 0};
  }
  for(int i = 0; i <= S.size(); i++) {
    dp[i][0] = (i == 0 ? 0 : 9 + 2 * (i - 1));
    if(i != 0) pos[i][0] = {0, 0};
  }

  for(int i = 0; i < S.size(); i++) {

    for(int j = 0; j < T.size(); j++) {
      int val = S[i] == T[j] ? dp[i][j] : dp[i][j] + 5;
      pair< int, int > rev = {i, j};

      int val2 = INF;
      pair< int, int > rev2 = {i, j};
      for(int k = poyo[i]; k <= poyo[i]; k++) {
        int cost = dp[i + 1][k] + 9 + 2 * (j - k);
        if(cost < val2) {
          val2 = cost;
          rev2 = {i + 1, k};
          poyo[i] = k;
        }
      }

      if(val2 < val) {
        val = val2;
        rev = rev2;
      }

      val2 = INF;
      rev2 = {i, j};

      for(int k = piyo[j]; k <= piyo[j]; k++) {
        int cost = dp[k][j + 1] + 9 + 2 * (i - k);
        if(cost < val2) {
          val2 = cost;
          rev2 = {k, j + 1};
          piyo[j] = k;
        }
      }
      if(val2 < val) {
        val = val2;
        rev = rev2;
      }

      dp[i + 1][j + 1] = min(dp[i + 1][j + 1], val);
      if(dp[piyo[j]][j + 1] + 9 + 2 * (i - piyo[j] + 1) > dp[i + 1][j + 1] + 9) piyo[j] = i + 1;
      if(dp[i + 1][poyo[i]] + 9 + 2 * (j - poyo[i] + 1) > dp[i + 1][j + 1] + 9) poyo[i] = j + 1;
      pos[i + 1][j + 1] = rev;
    }
  }

  cout << dp[S.size()][T.size()] << endl;
  pair< int, int > now = {S.size(), T.size()};

  string AA = "", BB = "";
  while(pos[now.first][now.second].first != -1) {
    auto qq = pos[now.first][now.second];
    if(qq.first + 1 == now.first && qq.second + 1 == now.second) {
      AA += S[qq.first];
      BB += T[qq.second];
    } else if(qq.first == now.first) {
      for(int j = 0; j < now.second - qq.second; j++) AA += "-";
      for(int j = 0; j < now.second - qq.second; j++) BB += T[now.second - j - 1];
    } else {
      for(int j = 0; j < now.first - qq.first; j++) BB += "-";
      for(int j = 0; j < now.first - qq.first; j++) AA += S[now.first - j - 1];
    }
    now = qq;
  }
  reverse(begin(AA), end(AA));
  reverse(begin(BB), end(BB));
  cout << AA << endl;
  cout << BB << endl;
}
0