結果

問題 No.874 正規表現間距離
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2019-08-31 11:02:46
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 28 ms / 2,000 ms
コード長 4,947 bytes
コンパイル時間 1,961 ms
コンパイル使用メモリ 206,408 KB
実行使用メモリ 19,332 KB
最終ジャッジ日時 2023-08-15 21:13:28
合計ジャッジ時間 3,908 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 4 ms
7,180 KB
testcase_17 AC 5 ms
7,172 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,384 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 12 ms
11,596 KB
testcase_26 AC 10 ms
11,660 KB
testcase_27 AC 26 ms
19,204 KB
testcase_28 AC 25 ms
17,868 KB
testcase_29 AC 24 ms
19,128 KB
testcase_30 AC 27 ms
19,332 KB
testcase_31 AC 27 ms
17,828 KB
testcase_32 AC 28 ms
17,860 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 10 ms
11,512 KB
testcase_37 AC 10 ms
11,528 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 1 ms
4,376 KB
testcase_40 AC 1 ms
4,376 KB
testcase_41 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
/*---------------------------------------------------------------------------------------------------
            ∧_∧
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     @hamayanhamayan
    /   \     | |
    /   / ̄ ̄ ̄ ̄/  |
  __(__ニつ/     _/ .| .|____
     \/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/














string A, B;
//---------------------------------------------------------------------------------------------------
enum {
    NORMAL,
    QUESTION,
    STAR
};
//---------------------------------------------------------------------------------------------------
vector<pair<int,char>> parse(string s) {
    int n = s.length();
    vector<pair<int,char>> res;
    rep(i, 0, n) {
        if(i + 1 < n and s[i + 1] == '?') {
            res.push_back({QUESTION, s[i]});
            i++;
        } else if(i + 1 < n and s[i + 1] == '*') {
            res.push_back({STAR, s[i]});
            i++;
        } else res.push_back({NORMAL, s[i]});
    }
    return res;
}
//---------------------------------------------------------------------------------------------------
int dp[2010][2010];
void _main() {
    cin >> A >> B;

    auto AA = parse(A);
    auto BB = parse(B);

    int NA = AA.size();
    int NB = BB.size();

    rep(a, 0, NA + 1) rep(b, 0, NB + 1) dp[a][b] = inf;
    dp[0][0] = 0;

    rep(a, 0, NA) rep(b, 0, NB) {
        if (AA[a].first == NORMAL) chmin(dp[a + 1][b], dp[a][b] + 1);
        else chmin(dp[a + 1][b], dp[a][b]); // ?の場合は0コストで消せる

        if (BB[b].first == NORMAL) chmin(dp[a][b + 1], dp[a][b] + 1);
        else chmin(dp[a][b + 1], dp[a][b]); // ?の場合は0コストで消せる

        if(AA[a].first == NORMAL and BB[b].first == NORMAL) {
            chmin(dp[a + 1][b + 1], dp[a][b] + (AA[a].second != BB[b].second));
        }
        if(AA[a].first == NORMAL and BB[b].first == QUESTION) {
            chmin(dp[a + 1][b + 1], dp[a][b] + (AA[a].second != BB[b].second));
        }
        if(AA[a].first == NORMAL and BB[b].first == STAR) {
            chmin(dp[a + 1][b + 1], dp[a][b] + (AA[a].second != BB[b].second));
            chmin(dp[a + 1][b], dp[a][b] + (AA[a].second != BB[b].second)); // *の場合は使っても遷移させないでOK
        }

        if(AA[a].first == QUESTION and BB[b].first == NORMAL) {
            chmin(dp[a + 1][b + 1], dp[a][b] + (AA[a].second != BB[b].second));
        }
        if(AA[a].first == QUESTION and BB[b].first == QUESTION) {
            chmin(dp[a + 1][b + 1], dp[a][b] + (AA[a].second != BB[b].second));
        }
        if(AA[a].first == QUESTION and BB[b].first == STAR) {
            chmin(dp[a + 1][b + 1], dp[a][b] + (AA[a].second != BB[b].second));
            chmin(dp[a + 1][b], dp[a][b] + (AA[a].second != BB[b].second)); // *の場合は使っても遷移させないでOK
        }

        if(AA[a].first == STAR and BB[b].first == NORMAL) {
            chmin(dp[a + 1][b + 1], dp[a][b] + (AA[a].second != BB[b].second));
            chmin(dp[a][b + 1], dp[a][b] + (AA[a].second != BB[b].second));
        }
        if(AA[a].first == STAR and BB[b].first == QUESTION) {
            chmin(dp[a + 1][b + 1], dp[a][b] + (AA[a].second != BB[b].second));
            chmin(dp[a][b + 1], dp[a][b] + (AA[a].second != BB[b].second));
        }
        if(AA[a].first == STAR and BB[b].first == STAR) {
            chmin(dp[a + 1][b + 1], dp[a][b] + (AA[a].second != BB[b].second));
            chmin(dp[a + 1][b], dp[a][b] + (AA[a].second != BB[b].second)); // *の場合は使っても遷移させないでOK
            chmin(dp[a][b + 1], dp[a][b] + (AA[a].second != BB[b].second)); // *の場合は使っても遷移させないでOK
        }
    }

    rep(a, 0, NA) {
        if (AA[a].first == NORMAL) chmin(dp[a + 1][NB], dp[a][NB] + 1);
        else chmin(dp[a + 1][NB], dp[a][NB]);
    }

    rep(b, 0, NB) {
        if (BB[b].first == NORMAL) chmin(dp[NA][b + 1], dp[NA][b] + 1);
        else chmin(dp[NA][b + 1], dp[NA][b]);
    }

    cout << dp[NA][NB] << endl;
}





0