結果

問題 No.422 文字列変更 (Hard)
ユーザー HachimoriHachimori
提出日時 2016-09-10 02:04:23
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 185 ms / 3,000 ms
コード長 2,550 bytes
コンパイル時間 653 ms
コンパイル使用メモリ 55,284 KB
実行使用メモリ 71,572 KB
最終ジャッジ日時 2023-08-10 21:07:39
合計ジャッジ時間 3,729 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
26,520 KB
testcase_01 AC 143 ms
69,468 KB
testcase_02 AC 145 ms
69,504 KB
testcase_03 AC 126 ms
69,464 KB
testcase_04 AC 143 ms
69,548 KB
testcase_05 AC 150 ms
69,632 KB
testcase_06 AC 176 ms
71,572 KB
testcase_07 AC 8 ms
26,524 KB
testcase_08 AC 18 ms
70,112 KB
testcase_09 AC 173 ms
71,484 KB
testcase_10 AC 185 ms
71,532 KB
testcase_11 AC 145 ms
69,548 KB
testcase_12 AC 131 ms
69,488 KB
testcase_13 AC 135 ms
69,488 KB
testcase_14 AC 137 ms
69,568 KB
testcase_15 AC 140 ms
69,620 KB
testcase_16 AC 146 ms
69,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<cstring>
using namespace std;
const int INF = 1<<30;
const int BUF = 1205;


enum Type {
    MATCH = 0,
    DELETE = 1,
    INSERT = 2
};

class Next {
public:
    int type, aPt, bPt;
    Next(){}
    Next(int type, int aPt, int bPt):
        type(type), aPt(aPt), bPt(bPt){}
};


int aLen, bLen;
string a, b;

void read() {
    cin >> aLen >> bLen;
    cin >> a >> b;
}


void rec(int preType, int aPos, int bPos, int dp[3][BUF][BUF], Next next[3][BUF][BUF]) {
    int &ret = dp[preType][aPos][bPos];
    Next &nex = next[preType][aPos][bPos];
    
    if (ret != -1) return;
    if (aPos == aLen && bPos == bLen) {
        ret = 0;
        return;
    }
    
    ret = INF;

    // match
    if (aPos < aLen && bPos < bLen) {
        rec(MATCH, aPos + 1, bPos + 1, dp, next);
        if (ret > dp[MATCH][aPos + 1][bPos + 1] + (a[aPos] == b[bPos] ? 0 : 5)) {
            ret = dp[MATCH][aPos + 1][bPos + 1] + (a[aPos] == b[bPos] ? 0 : 5);
            nex = Next(MATCH, aPos + 1, bPos + 1);
        }
    }

    // delete
    if (aPos < aLen) {
        rec(DELETE, aPos + 1, bPos, dp, next);
        if (ret > (preType == DELETE ? 2 : 9) + dp[DELETE][aPos + 1][bPos]) {
            ret = (preType == DELETE ? 2 : 9) + dp[DELETE][aPos + 1][bPos];
            nex = Next(DELETE, aPos + 1, bPos);
        }
    }
    
    // insert
    if (bPos < bLen) {
        rec(INSERT, aPos, bPos + 1, dp, next);
        if (ret > (preType == INSERT ? 2 : 9) + dp[INSERT][aPos][bPos + 1]) {
            ret = (preType == INSERT ? 2 : 9) + dp[INSERT][aPos][bPos + 1];
            nex = Next(INSERT, aPos, bPos + 1);
        }
    }
}


void work() {
    static int dp[3][BUF][BUF];
    static Next next[3][BUF][BUF];
    memset(dp, -1, sizeof(dp));
    rec(MATCH, 0, 0, dp, next);
    
    cout << dp[MATCH][0][0] << endl;

    int preType = MATCH;
    int aPos = 0;
    int bPos = 0;
    string aAns;
    string bAns;
    while (!(aPos == aLen && bPos == bLen)) {
        Next &nex = next[preType][aPos][bPos];

        switch (nex.type) {
        case MATCH:
            aAns += a[aPos];
            bAns += b[bPos];
            break;
        case DELETE:
            aAns += a[aPos];
            bAns += '-';
            break;
        case INSERT:
            aAns += '-';
            bAns += b[bPos];
            break;
        }
        
        preType = nex.type;
        aPos = nex.aPt;
        bPos = nex.bPt;
    }

    cout << aAns << endl;
    cout << bAns << endl;
}


int main() {
    read();
    work();
    return 0;
}
0