結果
| 問題 |
No.422 文字列変更 (Hard)
|
| コンテスト | |
| ユーザー |
rickytheta
|
| 提出日時 | 2016-09-09 23:16:59 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 85 ms / 3,000 ms |
| コード長 | 2,605 bytes |
| コンパイル時間 | 1,502 ms |
| コンパイル使用メモリ | 162,288 KB |
| 実行使用メモリ | 75,008 KB |
| 最終ジャッジ日時 | 2024-11-17 06:35:04 |
| 合計ジャッジ時間 | 3,152 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 16 |
ソースコード
#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;
}
rickytheta