結果
問題 | No.422 文字列変更 (Hard) |
ユーザー |
![]() |
提出日時 | 2016-09-09 22:57:40 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
WA
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 2,341 bytes |
コンパイル時間 | 1,383 ms |
コンパイル使用メモリ | 162,420 KB |
実行使用メモリ | 63,104 KB |
最終ジャッジ日時 | 2024-11-17 06:34:45 |
合計ジャッジ時間 | 7,367 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 10 WA * 6 |
ソースコード
#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-delif(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;//matchret = min(ret, dpdp(s+1,t+1,0,0)+((S[s]==T[t])?0:5));// delret = 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));// addret = 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;}