結果

問題 No.422 文字列変更 (Hard)
ユーザー 👑 kmjpkmjp
提出日時 2016-09-09 22:57:40
言語 C++11
(gcc 11.4.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,341 bytes
コンパイル時間 1,255 ms
コンパイル使用メモリ 149,292 KB
実行使用メモリ 63,280 KB
最終ジャッジ日時 2023-08-10 20:56:11
合計ジャッジ時間 6,511 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
62,828 KB
testcase_01 AC 271 ms
63,084 KB
testcase_02 AC 300 ms
62,912 KB
testcase_03 AC 257 ms
63,008 KB
testcase_04 AC 280 ms
62,912 KB
testcase_05 AC 310 ms
63,112 KB
testcase_06 AC 371 ms
63,032 KB
testcase_07 AC 17 ms
62,980 KB
testcase_08 AC 18 ms
63,044 KB
testcase_09 AC 359 ms
62,976 KB
testcase_10 AC 377 ms
62,976 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,M;
string S,T;
int dp[1300][1300][3][3];
int pat[1300][1300][3][3];

string RS,RT;

int dpdp(int s,int t,int st,int tt) { // 0-none 1-add 2-del
	if(s==N && t==M) return 0;
	int& ret=dp[s][t][st][tt];
	if(ret>=0) return ret;
	
	if(s==N) return ret=min((st==1)?0:7,(tt==2)?0:7)+(M-t)*2;
	if(t==M) return ret=min((st==2)?0:7,(tt==1)?0:7)+(N-s)*2;
	ret=10101010;
	//match
	ret = min(ret, dpdp(s+1,t+1,0,0)+((S[s]==T[t])?0:5));
	// del
	ret = min(ret, dpdp(s+1,t,2,tt)+((st==2)?2:9));
	ret = min(ret, dpdp(s,t+1,st,2)+((tt==2)?2:9));
	// add
	ret = min(ret, dpdp(s+1,t,st,1)+((tt==1)?2:9));
	ret = min(ret, dpdp(s,t+1,1,tt)+((st==1)?2:9));
	return ret;
}

void dfs(int s,int t,int st,int tt) {
	if(s==N && t==M) return;
	int& ret=dp[s][t][st][tt];
	
	if(s==N) {
		if(st==1) {
			while(t<M) {
				RS+=T[t];
				RT+=T[t];
				t++;
			}
		}
		else {
			while(t<M) {
				RS+='-';
				RT+=T[t];
				t++;
			}
		}
		return;
	}
	if(t==M) {
		if(st==2) {
			while(s<N) {
				RS+=S[s];
				RT+='-';
				s++;
			}
		}
		else {
			while(s<N) {
				RS+=S[s];
				RT+=S[s];
				s++;
			}
		}
		return;
	}
	if(ret==dpdp(s+1,t+1,0,0)+((S[s]==T[t])?0:5)) {
		RS+=S[s];
		RT+=T[t];
		dfs(s+1,t+1,0,0);
	}
	else if(ret==dpdp(s+1,t,2,tt)+((st==2)?2:9)) {
		RS+=S[s];
		RT+='-';
		dfs(s+1,t,2,tt);
	}
	else if(ret==dpdp(s,t+1,st,2)+((tt==2)?2:9)) {
		RS+='-';
		RT+=T[t];
		dfs(s,t+1,st,2);
	}
	else if(ret==dpdp(s+1,t,st,1)+((tt==1)?2:9)) {
		RS+=S[s];
		RT+=S[s];
		dfs(s+1,t,st,1);
	}
	else {
		RS+=T[t];
		RT+=T[t];
		dfs(s,t+1,1,st);
	}
}
	

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>M>>S>>T;
	MINUS(dp);
	
	cout<<dpdp(0,0,0,0)<<endl;
	dfs(0,0,0,0);
	cout<<RS<<endl<<RT<<endl;
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n';
	FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	solve(); return 0;
}
0