結果

問題 No.422 文字列変更 (Hard)
ユーザー ei1333333ei1333333
提出日時 2016-09-09 23:34:48
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2,464 ms / 3,000 ms
コード長 1,974 bytes
コンパイル時間 1,196 ms
コンパイル使用メモリ 150,776 KB
実行使用メモリ 20,444 KB
最終ジャッジ日時 2023-08-10 20:58:05
合計ジャッジ時間 25,674 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
20,336 KB
testcase_01 AC 1,415 ms
20,252 KB
testcase_02 AC 1,724 ms
20,320 KB
testcase_03 AC 1,269 ms
20,308 KB
testcase_04 AC 1,448 ms
20,280 KB
testcase_05 AC 1,784 ms
20,284 KB
testcase_06 AC 2,464 ms
20,332 KB
testcase_07 AC 10 ms
20,312 KB
testcase_08 AC 13 ms
20,320 KB
testcase_09 AC 2,168 ms
20,444 KB
testcase_10 AC 2,390 ms
20,324 KB
testcase_11 AC 1,441 ms
20,412 KB
testcase_12 AC 1,186 ms
20,264 KB
testcase_13 AC 1,285 ms
20,324 KB
testcase_14 AC 1,324 ms
20,256 KB
testcase_15 AC 1,368 ms
20,256 KB
testcase_16 AC 1,458 ms
20,320 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 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};
      for(int k = j; k >= 0; k--) {
        int cost = dp[i + 1][k] + 9 + 2 * (j - k);
        if(cost < val) {
          val = cost;
          rev = {i + 1, k};
        }
      }
      for(int k = i; k >= 0; k--) {
        int cost = dp[k][j + 1] + 9 + 2 * (i - k);
        if(cost < val) {
          val = cost;
          rev = {k, j + 1};
        }
      }
      dp[i + 1][j + 1] = min(dp[i + 1][j + 1], val);
      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