結果
| 問題 |
No.422 文字列変更 (Hard)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-09-10 02:04:23 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 197 ms / 3,000 ms |
| コード長 | 2,550 bytes |
| コンパイル時間 | 682 ms |
| コンパイル使用メモリ | 58,140 KB |
| 実行使用メモリ | 71,596 KB |
| 最終ジャッジ日時 | 2024-11-17 06:48:48 |
| 合計ジャッジ時間 | 3,755 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 16 |
ソースコード
#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;
}