結果

問題 No.422 文字列変更 (Hard)
ユーザー btkbtk
提出日時 2016-09-09 23:48:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 259 ms / 3,000 ms
コード長 2,889 bytes
コンパイル時間 1,649 ms
コンパイル使用メモリ 175,280 KB
実行使用メモリ 160,868 KB
最終ジャッジ日時 2023-08-10 21:00:14
合計ジャッジ時間 5,648 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 193 ms
110,764 KB
testcase_02 AC 213 ms
122,808 KB
testcase_03 AC 173 ms
101,588 KB
testcase_04 AC 189 ms
110,716 KB
testcase_05 AC 218 ms
128,644 KB
testcase_06 AC 259 ms
160,868 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 247 ms
146,996 KB
testcase_10 AC 258 ms
156,244 KB
testcase_11 AC 196 ms
112,888 KB
testcase_12 AC 173 ms
99,940 KB
testcase_13 AC 181 ms
104,644 KB
testcase_14 AC 191 ms
107,292 KB
testcase_15 AC 185 ms
108,452 KB
testcase_16 AC 196 ms
112,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;

struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}init;
#define N 0
#define D 1
#define I 2
typedef long long LL;
typedef vector<LL> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
const LL INF=1e15;
int main(){
    int n,m;
    cin>>n>>m;

    string S,T;
    cin>>S>>T;

    VVV dp(n+1,VV(m+1,V(3,INF)));
    VVV prev(n+1,VV(m+1,V(3,-1)));

    dp[0][0][0]=0;
    for(int i=0;i<=n;i++)
        for(int j=0;j<=m;j++){
            V &now=dp[i][j];
            if(i<n){//Delete
                V &nxt=dp[i+1][j];
                V &p=prev[i+1][j];
                
                if(nxt[D]>now[N]+9){
                    nxt[D]=now[N]+9;p[D]=N;
                }                
                if(nxt[D]>now[I]+9){
                    nxt[D]=now[I]+9;p[D]=I;
                }
                if(nxt[D]>now[D]+2){
                    nxt[D]=now[D]+2;p[D]=D;
                }
            }
            if(j<m){//Insert
                V &nxt=dp[i][j+1];
                V &p=prev[i][j+1];
                
                if(nxt[I]>now[N]+9){
                    nxt[I]=now[N]+9;p[I]=N;
                }                
                if(nxt[I]>now[I]+2){
                    nxt[I]=now[I]+2;p[I]=I;
                }
                if(nxt[I]>now[D]+9){
                    nxt[I]=now[D]+9;p[I]=D;
                }
            }
            if(i<n&&j<m){//Change
                V &nxt=dp[i+1][j+1];
                V &p=prev[i+1][j+1];
                int cost=0;
                if(S[i]!=T[j])cost=5;
                if(nxt[N]>now[N]+cost){
                    nxt[N]=now[N]+cost;p[N]=N;
                }                
                if(nxt[N]>now[I]+cost){
                    nxt[N]=now[I]+cost;p[N]=I;
                }
                if(nxt[N]>now[D]+cost){
                    nxt[N]=now[D]+cost;p[N]=D;
                    }
            }
        }
    LL res=INF;
    int pp=-1;
    for(int i=0;i<3;i++)if(res>dp[n][m][i]){
            res=dp[n][m][i];
            pp=i;
        }
    cout<<res<<endl;
    int s=n;
    int t=m;
    string cmd;
    while(s>0||t>0){
        int np=prev[s][t][pp];
        // cout<<s<<" "<<t<<endl;
        //cout<<dp[s][t][pp]<<endl;
        //cout<<pp<<"  "<<np<<endl;
        if(pp==D){
            s--;
            cmd+="D";
        }
        if(pp==I){
            t--;
            cmd+="I";
        }
        if(pp==N){
            s--,t--;
            cmd+="N";
        }
        pp=np;
    }
    string SS,TT;
    reverse(cmd.begin(),cmd.end());
    s=0,t=0;
    for(auto &it:cmd){
        if(it=='I'){
            SS+='-';
            TT+=T[t++];
        }
        if(it=='D'){
            TT+='-';
            SS+=S[s++];
        }
        if(it=='N'){
            SS+=S[s++];
            TT+=T[t++];
        }
    }
    cout<<SS<<endl;
    cout<<TT<<endl;
    return 0;
}
0