結果

問題 No.422 文字列変更 (Hard)
ユーザー ei1333333ei1333333
提出日時 2016-09-10 00:01:05
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 32 ms / 3,000 ms
コード長 2,122 bytes
コンパイル時間 1,369 ms
コンパイル使用メモリ 164,336 KB
実行使用メモリ 20,352 KB
最終ジャッジ日時 2024-04-28 14:12:16
合計ジャッジ時間 2,355 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
20,352 KB
testcase_01 AC 29 ms
20,224 KB
testcase_02 AC 30 ms
20,096 KB
testcase_03 AC 29 ms
20,224 KB
testcase_04 AC 30 ms
20,352 KB
testcase_05 AC 32 ms
20,352 KB
testcase_06 AC 25 ms
20,352 KB
testcase_07 AC 14 ms
20,352 KB
testcase_08 AC 14 ms
20,352 KB
testcase_09 AC 23 ms
20,352 KB
testcase_10 AC 29 ms
20,096 KB
testcase_11 AC 32 ms
20,352 KB
testcase_12 AC 28 ms
20,352 KB
testcase_13 AC 28 ms
20,224 KB
testcase_14 AC 32 ms
20,352 KB
testcase_15 AC 28 ms
20,352 KB
testcase_16 AC 30 ms
20,352 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 cost = dp[i + 1][poyo[i]] + 9 + 2 * (j - poyo[i]);
      if(cost < val) {
        val = cost;
        rev = {i + 1, poyo[i]};
      }
      cost = dp[piyo[j]][j + 1] + 9 + 2 * (i - piyo[j]);
      if(cost < val) {
        val = cost;
        rev = {piyo[j], j + 1};
      }
      dp[i + 1][j + 1] = min(dp[i + 1][j + 1], val);
      if(dp[piyo[j]][j + 1] + 2 * (i - piyo[j] + 1) > dp[i + 1][j + 1]) piyo[j] = i + 1;
      if(dp[i + 1][poyo[i]] + 2 * (j - poyo[i] + 1) > dp[i + 1][j + 1]) 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