結果

問題 No.422 文字列変更 (Hard)
ユーザー latte0119latte0119
提出日時 2016-09-09 23:02:22
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,132 bytes
コンパイル時間 1,351 ms
コンパイル使用メモリ 164,472 KB
実行使用メモリ 104,948 KB
最終ジャッジ日時 2024-04-28 14:06:14
合計ジャッジ時間 2,982 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
71,628 KB
testcase_01 AC 51 ms
100,716 KB
testcase_02 AC 51 ms
100,860 KB
testcase_03 AC 47 ms
100,652 KB
testcase_04 AC 49 ms
100,716 KB
testcase_05 AC 53 ms
100,856 KB
testcase_06 WA -
testcase_07 AC 20 ms
71,124 KB
testcase_08 AC 27 ms
104,040 KB
testcase_09 AC 47 ms
104,948 KB
testcase_10 AC 50 ms
104,824 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;

#define int long long
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define all(v) (v).begin(),(v).end()
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define pb push_back
#define fi first
#define se second
template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}


int N,M;
string S,T;


pint dp[1201][1201][3];
int pre[1201][1201][3];
signed main(){
    cin>>N>>M;
    cin>>S>>T;

    fill_n(**dp,1201*1201*3,pint(1001001001001001001ll,-1));
    dp[0][0][0]=pint(0,-1);
    for(int i=0;i<=N;i++){
        for(int j=0;j<=M;j++){
            chmin(dp[i][j][0],min(dp[i][j][1],dp[i][j][2]));
            rep(k,3){
                pre[i][j][k]=dp[i][j][k].se;
                dp[i][j][k].se=i*3*M+j*3+k;
            }
            if(i<N){
                chmin(dp[i+1][j][1],pint(dp[i][j][1].fi+2,dp[i][j][1].se));
                chmin(dp[i+1][j][1],pint(dp[i][j][0].fi+9,dp[i][j][0].se));
            }
            if(j<M){
                chmin(dp[i][j+1][2],pint(dp[i][j][2].fi+2,dp[i][j][2].se));
                chmin(dp[i][j+1][2],pint(dp[i][j][0].fi+9,dp[i][j][0].se));
            }
            if(i<N&&j<M){
                chmin(dp[i+1][j+1][0],pint(dp[i][j][0].fi+(S[i]==T[j]?0:5),dp[i][j][0].se));
            }
        }
    }

    cout<<dp[N][M][0].fi<<endl;
    string SS,TT;
    int i=N,j=M,k=0;
    while(pre[i][j][k]>=0){
        int ii=pre[i][j][k]/(3*M);
        pre[i][j][k]%=3*M;
        int jj=pre[i][j][k]/3;
        pre[i][j][k]%=3;
        int kk=pre[i][j][k];

        if(ii<i&&jj<j){
            SS+=S[ii];
            TT+=T[jj];
        }
        else if(ii<i){
            SS+=S[ii];
            TT+='-';
        }
        else if(jj<j){
            SS+='-';
            TT+=T[jj];
        }
        i=ii;j=jj;k=kk;
    }

    reverse(all(SS));reverse(all(TT));
    cout<<SS<<endl;
    cout<<TT<<endl;
    return 0;
}
0