結果

問題 No.422 文字列変更 (Hard)
ユーザー rickythetarickytheta
提出日時 2016-09-09 23:16:59
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 53 ms / 3,000 ms
コード長 2,605 bytes
コンパイル時間 1,194 ms
コンパイル使用メモリ 148,588 KB
実行使用メモリ 77,232 KB
最終ジャッジ日時 2023-08-10 20:56:29
合計ジャッジ時間 2,956 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
32,188 KB
testcase_01 AC 47 ms
77,196 KB
testcase_02 AC 50 ms
77,048 KB
testcase_03 AC 45 ms
77,080 KB
testcase_04 AC 47 ms
77,072 KB
testcase_05 AC 52 ms
77,128 KB
testcase_06 AC 46 ms
77,084 KB
testcase_07 AC 10 ms
27,936 KB
testcase_08 AC 20 ms
75,572 KB
testcase_09 AC 52 ms
77,088 KB
testcase_10 AC 53 ms
77,072 KB
testcase_11 AC 48 ms
77,116 KB
testcase_12 AC 45 ms
77,232 KB
testcase_13 AC 46 ms
77,044 KB
testcase_14 AC 46 ms
77,128 KB
testcase_15 AC 47 ms
77,076 KB
testcase_16 AC 47 ms
77,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef int _loop_int;
#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)
#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)
#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

#define CHMIN(a,b) a=min((a),(b))
#define CHMAX(a,b) a=max((a),(b))

// mod
const ll MOD = 1000000007ll;
#define FIX(a) ((a)%MOD+MOD)%MOD

// floating
typedef double Real;
const Real EPS = 1e-11;
#define EQ0(x) (abs(x)<EPS)
#define EQ(a,b) (abs(a-b)<EPS)
typedef complex<Real> P;

int n,m;
string s,t;

//                 ↓ 0:normal, 1:deleting, 2:adding
int dp[1252][1252][3];
int befi[1252][1252][3];
int befj[1252][1252][3];
int befk[1252][1252][3];

int main(){
  cin>>n>>m;
  cin>>s>>t;
  REP(i,1252)REP(j,1252)REP(k,3)dp[i][j][k]=830252521;
  dp[0][0][0] = 0;
  REP(i,n+1)REP(j,m+1)REP(k,3){
    // modify
    if(i<n && j<m){
      int ncost = dp[i][j][k] + (s[i]==t[j]?0:5);
      if(ncost < dp[i+1][j+1][0]){
        dp[i+1][j+1][0] = ncost;
        befi[i+1][j+1][0] = i;
        befj[i+1][j+1][0] = j;
        befk[i+1][j+1][0] = k;
      }
    }
    // delete
    if(i<n){
      int ncost = dp[i][j][k] + (k==1?2:9);
      if(ncost < dp[i+1][j][1]){
        dp[i+1][j][1] = ncost;
        befi[i+1][j][1] = i;
        befj[i+1][j][1] = j;
        befk[i+1][j][1] = k;
      }
    }
    // add
    if(j<m){
      int ncost = dp[i][j][k] + (k==2?2:9);
      if(ncost < dp[i][j+1][2]){
        dp[i][j+1][2] = ncost;
        befi[i][j+1][2] = i;
        befj[i][j+1][2] = j;
        befk[i][j+1][2] = k;
      }
    }
  }
  // DEBUG(dp[n][m][0]);
  // DEBUG(dp[n][m][1]);
  // DEBUG(dp[n][m][2]);
  int curn = n;
  int curm = m;
  int curk = 0;
  if(dp[n][m][1] < dp[n][m][curk])curk=1;
  if(dp[n][m][2] < dp[n][m][curk])curk=2;
  printf("%d\n",dp[n][m][curk]);
  // fukugen tsurai
  string a,b;
  while(curn!=0 || curm!=0 || curk!=0){
    int i = befi[curn][curm][curk];
    int j = befj[curn][curm][curk];
    int k = befk[curn][curm][curk];
    if(curk==0){
      // modify
      a = s[i]+a;
      b = t[j]+b;
    }else if(curk==1){
      // delete
      a = s[i]+a;
      b = '-'+b;
    }else{
      // add
      a = '-'+a;
      b = t[j]+b;
    }
    curn=i;
    curm=j;
    curk=k;
  }
  cout<<a<<endl;
  cout<<b<<endl;
  return 0;
}
0