結果

問題 No.874 正規表現間距離
ユーザー kakira9618kakira9618
提出日時 2019-05-01 22:13:00
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,398 bytes
コンパイル時間 1,855 ms
コンパイル使用メモリ 171,320 KB
実行使用メモリ 19,160 KB
最終ジャッジ日時 2023-08-11 04:15:51
合計ジャッジ時間 4,217 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 10 ms
7,380 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 36 ms
18,996 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 35 ms
18,920 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 36 ms
18,960 KB
testcase_33 WA -
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 WA -
testcase_37 AC 10 ms
7,292 KB
testcase_38 AC 1 ms
4,380 KB
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
#define chmin(x, y) (x) = min((x), (y))

bool isMark(char c) {
    if (c == '?' || c == '*') return true;
    return false;
}

void pre(string &s) {
    int N = s.size();
    for(int i = 0; i + 1 < N; i++) {
        if (!isMark(s[i]) && isMark(s[i + 1])) {
            swap(s[i], s[i + 1]);
        }
    }
    s += "$"; // 番兵.
}

int main() {
    string A, B;
    cin >> A >> B;
    int N = A.size(), M = B.size();
    pre(A); pre(B);

    vector<vector<int>> dp(N + 1, vector<int>(M + 1, 1 << 29));
    dp[0][0] = 0;
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < M; j++) {
            if (!isMark(A[i]) && !isMark(B[j])) {
                chmin(dp[i + 1][j], dp[i][j] + 1);
                chmin(dp[i][j + 1], dp[i][j] + 1);
                chmin(dp[i + 1][j + 1], dp[i][j] + (A[i] != B[j]));
            } else if (A[i] == '?' && !isMark(B[j])) {
                chmin(dp[i + 2][j], dp[i][j]); // ? でのマッチをやめる.
                chmin(dp[i][j + 1], dp[i][j] + 1); // ?を温存して次の文字とマッチさせる
                chmin(dp[i + 2][j + 1], dp[i][j] + (A[i + 1] != B[j])); // ?でのマッチを行う.*と違い,?のマッチを行うとiが進む(2回同じ?は使えない)
            } else if (!isMark(A[i]) && B[j] == '?') {
                chmin(dp[i][j + 2], dp[i][j]);
                chmin(dp[i + 1][j], dp[i][j] + 1);
                chmin(dp[i + 1][j + 2], dp[i][j] + (A[i] != B[j + 1]));
            } else if (A[i] == '*' && !isMark(B[j])) {
                chmin(dp[i + 2][j], dp[i][j]); // * でのマッチをやめる
                chmin(dp[i][j + 1], dp[i][j] + 1); // * を温存して次の文字とマッチさせる
                chmin(dp[i][j + 1], dp[i][j] + (A[i + 1] != B[j])); // *でのマッチを行う.この時,iが進まないので,再度この*でのマッチが行われる
            } else if (!isMark(A[i]) && B[j] == '*') {
                chmin(dp[i][j + 2], dp[i][j]);
                chmin(dp[i + 1][j], dp[i][j] + 1);
                chmin(dp[i + 1][j], dp[i][j] + (A[i] != B[j + 1]));
            } else {
                chmin(dp[i + 2][j], dp[i][j]); // B側のマークを残す
                chmin(dp[i][j + 2], dp[i][j]); // A側のマークを残す
            }
        }
    }
    cout << dp[N][M] << endl;
    return 0;
}
0