結果

問題 No.422 文字列変更 (Hard)
ユーザー HachimoriHachimori
提出日時 2016-09-10 02:04:23
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 190 ms / 3,000 ms
コード長 2,550 bytes
コンパイル時間 765 ms
コンパイル使用メモリ 58,532 KB
実行使用メモリ 71,508 KB
最終ジャッジ日時 2024-04-28 14:16:30
合計ジャッジ時間 3,598 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
20,736 KB
testcase_01 AC 164 ms
62,976 KB
testcase_02 AC 153 ms
64,848 KB
testcase_03 AC 134 ms
64,464 KB
testcase_04 AC 144 ms
64,852 KB
testcase_05 AC 158 ms
63,436 KB
testcase_06 AC 187 ms
71,380 KB
testcase_07 AC 8 ms
21,328 KB
testcase_08 AC 18 ms
35,072 KB
testcase_09 AC 174 ms
69,120 KB
testcase_10 AC 190 ms
71,508 KB
testcase_11 AC 146 ms
63,316 KB
testcase_12 AC 135 ms
63,948 KB
testcase_13 AC 137 ms
63,436 KB
testcase_14 AC 139 ms
64,840 KB
testcase_15 AC 139 ms
64,336 KB
testcase_16 AC 148 ms
64,720 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