結果

問題 No.422 文字列変更 (Hard)
ユーザー 👑 kmjpkmjp
提出日時 2016-09-10 00:17:25
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,567 bytes
コンパイル時間 1,459 ms
コンパイル使用メモリ 160,616 KB
実行使用メモリ 23,424 KB
最終ジャッジ日時 2024-04-28 14:13:47
合計ジャッジ時間 3,342 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 14 ms
23,296 KB
testcase_09 AC 95 ms
23,296 KB
testcase_10 AC 94 ms
23,296 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];
int pat[1300][1300][3];

string RS,RT;

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

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

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>M>>S>>T;
	MINUS(dp);
	
	cout<<dpdp(0,0,0)<<endl;
	dfs(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