結果

問題 No.874 正規表現間距離
ユーザー finefine
提出日時 2019-08-30 23:00:38
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 1,983 bytes
コンパイル時間 1,748 ms
コンパイル使用メモリ 173,796 KB
実行使用メモリ 18,868 KB
最終ジャッジ日時 2023-08-14 07:19:50
合計ジャッジ時間 3,457 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,384 KB
testcase_16 AC 4 ms
4,384 KB
testcase_17 AC 4 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 13 ms
8,288 KB
testcase_26 AC 9 ms
8,380 KB
testcase_27 AC 26 ms
18,836 KB
testcase_28 AC 19 ms
14,128 KB
testcase_29 AC 26 ms
18,868 KB
testcase_30 AC 26 ms
18,856 KB
testcase_31 AC 22 ms
15,916 KB
testcase_32 AC 22 ms
15,936 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 7 ms
6,636 KB
testcase_37 AC 7 ms
6,428 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 1 ms
4,376 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<char, char>;

vector<P> calc(string& s) {
    int n = s.size();
    vector<P> res;
    for (int i = 0; i < n; ) {
        if (i < n - 1) {
            if (s[i + 1] == '?' || s[i + 1] == '*') {
                res.emplace_back(s[i], s[i + 1]);
                i += 2;
            } else {
                res.emplace_back(s[i], s[i]);
                i++;
            }
        } else {
            res.emplace_back(s[i], s[i]);
            break;
        }
    }
    return res;
}

const int INF = 1e9;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    string a, b;
    cin >> a >> b;

    vector<P> va = calc(a);
    vector<P> vb = calc(b);

    int na = va.size();
    int nb = vb.size();

    vector< vector<int> > dp(na + 1, vector<int>(nb + 1, INF));
    dp[0][0] = 0;
    for (int i = 1; i <= na; i++) {
        dp[i][0] = dp[i - 1][0];
        if (va[i - 1].second == '?' || va[i - 1].second == '*') continue;
        dp[i][0]++;
    }

    for (int i = 1; i <= nb; i++) {
        dp[0][i] = dp[0][i - 1];
        if (vb[i - 1].second == '?' || vb[i - 1].second == '*') continue;
        dp[0][i]++;
    }

    for (int i = 0; i < na; i++) {
        for (int j = 0; j < nb; j++) {
            int cost = (va[i].first != vb[j].first);
            int hoge = dp[i][j];
            if (va[i].second == '*') hoge = min(hoge, dp[i + 1][j]);
            if (vb[j].second == '*') hoge = min(hoge, dp[i][j + 1]);
            dp[i + 1][j + 1] = min({
                dp[i + 1][j + 1],
                hoge + cost,
                dp[i][j + 1] + (va[i].first == va[i].second),
                dp[i + 1][j] + (vb[j].first == vb[j].second)
            });
        }
    }

    /*
    for (int i = 0; i <= na; i++) {
        for (int j = 0; j <= nb; j++) {
            cerr << i << " " << j << ": " << dp[i][j] << endl;
        }
    }//*/
    cout << dp[na][nb] << endl;
    return 0;
}
0