結果

問題 No.874 正規表現間距離
ユーザー NOSSNOSS
提出日時 2019-08-30 23:09:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 38 ms / 2,000 ms
コード長 2,446 bytes
コンパイル時間 3,937 ms
コンパイル使用メモリ 171,644 KB
実行使用メモリ 18,916 KB
最終ジャッジ日時 2023-08-14 07:49:19
合計ジャッジ時間 3,594 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 1 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,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 5 ms
4,376 KB
testcase_17 AC 6 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 18 ms
8,304 KB
testcase_26 AC 14 ms
8,468 KB
testcase_27 AC 37 ms
18,864 KB
testcase_28 AC 30 ms
14,108 KB
testcase_29 AC 38 ms
18,916 KB
testcase_30 AC 37 ms
18,864 KB
testcase_31 AC 35 ms
15,984 KB
testcase_32 AC 34 ms
16,120 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 10 ms
6,480 KB
testcase_37 AC 10 ms
6,452 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 1 ms
4,376 KB
testcase_41 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll, P> P3;
typedef pair<P, P> PP;
constexpr ll MOD = ll(1e9 + 7);
constexpr int IINF = INT_MAX;
constexpr ll LLINF = LLONG_MAX;
constexpr int MAX_N = int(1e5) + 5;
constexpr double EPS = 1e-9;
constexpr int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define ALL(v) (v).begin(), (v).end()

int solve(vector<int> &a, vector<int> &b){
    int n = a.size(), m = b.size();
    vector<vector<int> > dp(n+1, vector<int>(m+1, IINF));
    dp[0][0] = 0;
    
    for(int i=0;i<n;i++){
        dp[i+1][0] = dp[i][0] + (a[i] < 26);
    }
    for(int j=0;j<m;j++){
        dp[0][j+1] = dp[0][j] + (b[j] < 26);
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(a[i]%26 == b[j]%26){
                dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j]);
            }
            else{
                dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j]+1);
            }
            dp[i+1][j+1] = min({dp[i+1][j+1], dp[i+1][j] + 1, dp[i][j+1] + 1});
            if(a[i]>=26){
                dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j+1]);
            }
            if(b[j]>=26){
                dp[i+1][j+1] = min(dp[i+1][j+1], dp[i+1][j]);
            }
            if(a[i]>=52){
                dp[i+1][j+1] = min(dp[i+1][j+1], dp[i+1][j] + (a[i]%26!=b[j]%26));
            }
            if(b[j]>=52){
                dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j+1] + (a[i]%26!=b[j]%26));
            }
        }
    }
    return dp[n][m];
}

int main() {
    int n, m;
    string A, B;
    cin >> A >> B;
    vector<int> a, b;
    n = A.size();
    m = B.size();
    A.push_back('#');
    B.push_back('#');
    for(int i=0;i<n;i++){
        int c = A[i]-'a';
        if(A[i+1] == '?'){
            c += 26;
            i++;
        }
        else if(A[i+1] == '*'){
            c += 52;
            i++;
        }
        a.push_back(c);
    }
    for(int i=0;i<m;i++){
        int c = B[i]-'a';
        if(B[i+1] == '?'){
            c += 26;
            i++;
        }
        else if(B[i+1] == '*'){
            c += 52;
            i++;
        }
        b.push_back(c);
    }
    cout << solve(a, b) << endl;
    return 0;
}
0