結果

問題 No.422 文字列変更 (Hard)
ユーザー parukiparuki
提出日時 2016-09-10 00:03:49
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 67 ms / 3,000 ms
コード長 3,332 bytes
コンパイル時間 1,592 ms
コンパイル使用メモリ 170,224 KB
実行使用メモリ 116,208 KB
最終ジャッジ日時 2023-08-10 21:02:57
合計ジャッジ時間 3,519 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
11,744 KB
testcase_01 AC 57 ms
106,024 KB
testcase_02 AC 61 ms
105,888 KB
testcase_03 AC 54 ms
105,832 KB
testcase_04 AC 57 ms
105,956 KB
testcase_05 AC 62 ms
105,944 KB
testcase_06 AC 59 ms
116,208 KB
testcase_07 AC 3 ms
11,768 KB
testcase_08 AC 25 ms
114,780 KB
testcase_09 AC 63 ms
116,132 KB
testcase_10 AC 67 ms
116,184 KB
testcase_11 AC 58 ms
106,172 KB
testcase_12 AC 52 ms
105,808 KB
testcase_13 AC 55 ms
105,900 KB
testcase_14 AC 55 ms
105,924 KB
testcase_15 AC 54 ms
105,888 KB
testcase_16 AC 55 ms
105,876 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
#define mt make_tuple

string nstr(){
    static const int MAX_LEN = 1300;
    static char res_[MAX_LEN];
    scanf("%s",res_);
    return string(res_);
}


typedef tuple<int, short, short, short, short> P;
// Sのi文字目まで見た.Tのj文字目まで見た.removeしているか。挿入しているか。
int dp[1201][1201][2][2];
int ppi[1201][1201][2][2];
int ppj[1201][1201][2][2];
int ppk[1201][1201][2][2];
int ppl[1201][1201][2][2];

int main(){
    int N, M;
    cin >> N >> M;
    string S = nstr(), T = nstr();
    const int INF = INT_MAX;
    rep(i, N + 1)rep(j, M + 1)rep(k, 2)rep(l, 2)dp[i][j][k][l] = INF;

    dp[0][0][0][0] = 0;
    rep(i, N+1)rep(j, M+1)rep(k, 2)rep(l,2){
        int x;
        x = dp[i][j][k][l];
        if(x == INF)continue;
        auto f = [&](int y, int ni, int nj, int nk, int nl){
            if(dp[ni][nj][nk][nl] > y){
                dp[ni][nj][nk][nl] = y;
                ppi[ni][nj][nk][nl] = i;
                ppj[ni][nj][nk][nl] = j;
                ppk[ni][nj][nk][nl] = k;
                ppl[ni][nj][nk][nl] = l;
            }
        };
        // もともと一致
        if(i < N&&j < M && S[i]==T[j]){
            f(x, i + 1, j + 1, 0, 0);
            // smin(dp[i + 1][j + 1][0][0], x);
        }

        // 置換
        if(i < N&&j < M){
            f(x + 5, i + 1, j + 1, 0, 0);
            // min(dp[i + 1][j + 1][0][0], x+5);
        }
        // 挿入
        if(j < M){
            if(l == 0){
                // smin(dp[i][j + 1][0][1], x+9);
                f(x + 9, i, j + 1, 0, 1);
            } else{
                // smin(dp[i][j + 1][0][1], x+2);
                f(x+2, i, j + 1, 0, 1);
            }
        }
        // 削除
        if(i < N){
            if(k == 0){
                // smin(dp[i + 1][j][1][0], x+9);
                f(x + 9, i + 1, j, 1, 0);
            } else{
                // smin(dp[i + 1][j][1][0], x+2);
                f(x + 2, i + 1, j, 1, 0);
            }
        }
    }
    int mi = INF;
    rep(k, 2)rep(l, 2)smin(mi, dp[N][M][k][l]);
    int ii = N, jj = M, kk = -1, ll = -1;
    rep(k, 2)rep(l, 2)if(dp[N][M][k][l] == mi){
        kk = k;
        ll = l;
    }

    string ss, tt;
    while(ii > 0 || jj > 0){
        int pi, pj, pk, pl;
        pi = ppi[ii][jj][kk][ll];
        pj = ppj[ii][jj][kk][ll];
        pk = ppk[ii][jj][kk][ll];
        pl = ppl[ii][jj][kk][ll];
        if(pi == ii - 1 && pj == jj - 1){
            ss += S[pi];
            tt += T[pj];
        } else if(pi == ii - 1){
            ss += S[pi];
            tt += '-';
        } else{
            ss += '-';
            tt += T[pj];
        }
        ii = pi;
        jj = pj;
        kk = pk;
        ll = pl;
    }
    reverse(all(ss));
    reverse(all(tt));
    printf("%d\n%s\n%s\n", mi, ss.c_str(), tt.c_str());
}
0