結果

問題 No.874 正規表現間距離
ユーザー kakira9618kakira9618
提出日時 2019-08-19 22:03:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,872 bytes
コンパイル時間 1,747 ms
コンパイル使用メモリ 172,508 KB
実行使用メモリ 18,972 KB
最終ジャッジ日時 2023-08-11 04:16:53
合計ジャッジ時間 3,353 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#include <cassert>

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

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

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 += "$"; // 番兵.
}

bool validate(string s) {
    if (s.size() < 1 || s.size() > 2000) return false;
    if (isMark(s[0])) return false;
    for (int i = 0; i + 1 < s.size(); i++) {
        if (isMark(s[i]) && isMark(s[i + 1])) return false;
    }
    return true;
}

int main() {
    string A, B;
    cin >> A >> B;
    assert(validate(A) && validate(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])) {
                if (i + 1 <= N) chmin(dp[i + 1][j], dp[i][j] + 1);
                if (j + 1 <= M) chmin(dp[i][j + 1], dp[i][j] + 1);
                if (i + 1 <= N && j + 1 <= M) 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]); // ? でのマッチをやめる.
                if (j + 1 <= M) chmin(dp[i][j + 1], dp[i][j] + 1); // ?を温存して次の文字とマッチさせる
                //if (j + 1 <= M) 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]);
                if (i + 1 <= N) chmin(dp[i + 1][j], dp[i][j] + 1);
                if (i + 1 <= N) 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]); // * でのマッチをやめる
                if (j + 1 <= M) chmin(dp[i][j + 1], dp[i][j] + 1); // * を温存して次の文字とマッチさせる
                if (j + 1 <= M) 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]);
                if (i + 1 <= N) chmin(dp[i + 1][j], dp[i][j] + 1);
                if (i + 1 <= N) 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