結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
23,168 KB
testcase_01 AC 72 ms
23,296 KB
testcase_02 AC 82 ms
23,296 KB
testcase_03 AC 71 ms
23,296 KB
testcase_04 AC 73 ms
23,296 KB
testcase_05 AC 85 ms
23,296 KB
testcase_06 AC 88 ms
23,296 KB
testcase_07 AC 8 ms
23,296 KB
testcase_08 AC 7 ms
23,296 KB
testcase_09 AC 91 ms
23,296 KB
testcase_10 AC 96 ms
23,296 KB
testcase_11 AC 77 ms
23,296 KB
testcase_12 AC 68 ms
23,296 KB
testcase_13 AC 73 ms
23,296 KB
testcase_14 AC 73 ms
23,296 KB
testcase_15 AC 73 ms
23,424 KB
testcase_16 AC 79 ms
23,296 KB
権限があれば一括ダウンロードができます

ソースコード

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+='-', 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