結果

問題 No.874 正規表現間距離
ユーザー tactac
提出日時 2019-09-03 16:38:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 34 ms / 2,000 ms
コード長 2,730 bytes
コンパイル時間 2,627 ms
コンパイル使用メモリ 169,072 KB
実行使用メモリ 19,300 KB
最終ジャッジ日時 2023-08-25 19:55:57
合計ジャッジ時間 5,628 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
19,060 KB
testcase_01 AC 7 ms
19,284 KB
testcase_02 AC 7 ms
19,000 KB
testcase_03 AC 7 ms
19,000 KB
testcase_04 AC 7 ms
18,984 KB
testcase_05 AC 6 ms
19,128 KB
testcase_06 AC 7 ms
19,052 KB
testcase_07 AC 7 ms
19,036 KB
testcase_08 AC 7 ms
18,984 KB
testcase_09 AC 7 ms
19,032 KB
testcase_10 AC 7 ms
18,992 KB
testcase_11 AC 7 ms
19,164 KB
testcase_12 AC 7 ms
19,052 KB
testcase_13 AC 7 ms
19,044 KB
testcase_14 AC 7 ms
18,996 KB
testcase_15 AC 6 ms
19,072 KB
testcase_16 AC 11 ms
19,064 KB
testcase_17 AC 11 ms
19,004 KB
testcase_18 AC 7 ms
18,992 KB
testcase_19 AC 6 ms
19,056 KB
testcase_20 AC 7 ms
19,160 KB
testcase_21 AC 7 ms
18,976 KB
testcase_22 AC 6 ms
18,976 KB
testcase_23 AC 6 ms
19,300 KB
testcase_24 AC 7 ms
19,288 KB
testcase_25 AC 23 ms
19,040 KB
testcase_26 AC 21 ms
18,996 KB
testcase_27 AC 26 ms
19,008 KB
testcase_28 AC 32 ms
19,072 KB
testcase_29 AC 28 ms
19,176 KB
testcase_30 AC 31 ms
19,044 KB
testcase_31 AC 34 ms
19,012 KB
testcase_32 AC 34 ms
19,288 KB
testcase_33 AC 7 ms
19,196 KB
testcase_34 AC 7 ms
18,996 KB
testcase_35 AC 7 ms
19,036 KB
testcase_36 AC 13 ms
19,040 KB
testcase_37 AC 13 ms
19,088 KB
testcase_38 AC 7 ms
19,044 KB
testcase_39 AC 6 ms
18,992 KB
testcase_40 AC 7 ms
18,996 KB
testcase_41 AC 7 ms
19,044 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, n) for (int i = l; i < (n); ++i)
#define sz(v) (int)v.size()
#define inf (int)(1e9+7)
#define abs(x) (x >= 0 ? x : -(x))
#define ceil(a, b) a / b + !!(a % b)
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }
ll pow(ll a, int b) { return b ? pow(a * a, b / 2) * (b % 2 ? a : 1) : 1; }
template<typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); }




int dp[2002][2002];
int main() {
    
    
    string s1, s2;
    cin >> s1 >> s2;
    rep(i, sz(s1)) if (s1[i] == '?' || s1[i] == '*') swap(s1[i], s1[i - 1]);
    rep(i, sz(s2)) if (s2[i] == '?' || s2[i] == '*') swap(s2[i], s2[i - 1]);
    // cout << s1 << " " << s2 << endl;
    
    fill_n(*dp, 2002 * 2002, inf);
    dp[0][0] = 0;
    
    rep(i, sz(s1) + 1) rep(j, sz(s2) + 1) {
        int a = 0, b = 0;
        if (s1[i] == '?') a = 1;
        else if (s1[i] == '*') a = 2;
        if (s2[j] == '?') b = 1;
        else if (s2[j] == '*') b = 2;
        
        if (a == 0 && b == 0) {
            /* 1 */ chmin(dp[i + 1][j], dp[i][j] + 1);
            /* 2 */ chmin(dp[i][j + 1], dp[i][j] + 1);
            /* 3 */ chmin(dp[i + 1][j + 1], dp[i][j] + (s1[i] != s2[j]));
        } else if (a == 1 && b == 0) {
            /* 1 */ chmin(dp[i + 2][j], dp[i][j]);
            /* 2 */ chmin(dp[i][j + 1], dp[i][j] + 1);
            /* 3 */ chmin(dp[i + 2][j + 1], dp[i][j] + (s1[i + 1] != s2[j]));
        } else if (a == 0 && b == 1) { // 双対
            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] + (s1[i] != s2[j + 1]));
        } else if (a == 2 && b == 0) {
            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] + (s1[i + 1] != s2[j]));
            chmin(dp[i + 2][j + 1], dp[i][j] + 1);
        } else if (a == 0 && b == 2) { // 双対
            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] + (s1[i] != s2[j + 1]));
            chmin(dp[i + 1][j + 2], dp[i][j] + 1);
        } else {
            chmin(dp[i + 2][j], dp[i][j]);
            chmin(dp[i][j + 2], dp[i][j]);
        }
        
    }
    
    cout << dp[sz(s1)][sz(s2)] << endl;
    
    // rep(i, sz(s1) + 1) { rep(j, sz(s2) + 1) cout << dp[i][j] << " "; cout << endl; }
}
0