結果

問題 No.874 正規表現間距離
ユーザー petite_progpetite_prog
提出日時 2020-04-18 11:56:19
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 37 ms / 2,000 ms
コード長 4,493 bytes
コンパイル時間 1,884 ms
コンパイル使用メモリ 174,620 KB
実行使用メモリ 18,944 KB
最終ジャッジ日時 2024-04-14 20:40:51
合計ジャッジ時間 3,180 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 1 ms
6,940 KB
testcase_16 AC 4 ms
6,940 KB
testcase_17 AC 5 ms
6,940 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 1 ms
6,944 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 1 ms
6,940 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 12 ms
8,704 KB
testcase_26 AC 11 ms
8,704 KB
testcase_27 AC 29 ms
18,944 KB
testcase_28 AC 31 ms
14,336 KB
testcase_29 AC 30 ms
18,816 KB
testcase_30 AC 36 ms
18,944 KB
testcase_31 AC 37 ms
16,256 KB
testcase_32 AC 37 ms
16,384 KB
testcase_33 AC 2 ms
6,940 KB
testcase_34 AC 2 ms
6,940 KB
testcase_35 AC 2 ms
6,940 KB
testcase_36 AC 10 ms
6,940 KB
testcase_37 AC 11 ms
6,944 KB
testcase_38 AC 2 ms
6,944 KB
testcase_39 AC 1 ms
6,940 KB
testcase_40 AC 2 ms
6,940 KB
testcase_41 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
   ∫ ∫ ∫
   ノヽ
  (_  )
 (_    )
(______ )
 ヽ(´・ω・)ノ 
   |  /
   UU
*/
#pragma region macro
#include <bits/stdc++.h>
typedef long long int64;
using namespace std;
typedef vector<int> vi;
const int MOD = (int)1e9 + 7;
const int64 INF = 1LL << 62;
const int inf = 1<<30;
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; }
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i,s,n) for (int i = s; i < (n); i++)
#define ALL(obj) (obj).begin(), (obj).end() //コンテナじゃないと使えない!!
#define debug(x) cerr << #x << ": " << x << "\n";
#define mp make_pair
#define bn '\n'
template <typename T>
ostream& operator<<(ostream& os, const vector<T> &V){
    int N = V.size();
    REP(i,N){
        os << V[i];
        if (i!=N-1) os << " ";
    }
    os << "\n";
    return os;
}
template <typename T,typename S>
ostream& operator<<(ostream& os, pair<T,S> const&P){
    os << "(";
    os << P.first;
    os << " , ";
    os << P.second;
    os << ")";
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, set<T> &S){
    auto it=S.begin();
    while(it!=S.end()){
        os << *it;
        os << " ";
        it++;
    }
    os << "\n";
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, deque<T> &q){
    for(auto it=q.begin();it<q.end();it++){
        os<<*it;
        os<<" ";
    }
    os<<endl;
    return os;
}
template <typename T,typename S>
ostream& operator<<(ostream& os, map<T,S> const&M){
    for(auto e:M){
        os<<e;
    }
    os<<endl;
    return os;
}
vector<pair<int,int>> dxdy = {mp(0,1),mp(1,0),mp(-1,0),mp(0,-1)};
#pragma endregion
//fixed<<setprecision(10)<<ans<<endl;



int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    string U,V;
    cin >> U >> V;
    string S="",T="";
    vector<int> S_info,T_info;
    for(auto s:U){
        if(s=='?'){
            S_info.back()=1;
        }else if(s=='*') S_info.back()=2;
        else{
            S_info.emplace_back(0);
            S+=s;
        }
    }
    for(auto s:V){
        if(s=='?'){
            T_info.back()=1;
        }else if(s=='*') T_info.back()=2;
        else {
            T_info.emplace_back(0);
            T+=s;
        }
    }

    int N=S.size(), M=T.size();

    vector<vector<int>>  DP(N+1,vector<int>(M+1,inf));
    DP[0][0] = 0;
    REP(i,N){
        REP(j,M){
            chmin(DP[i+1][j],DP[i][j]+1);
            chmin(DP[i][j+1],DP[i][j]+1);
            if(S_info[i]==0 and T_info[j]==0){ //s t
                if(S[i]==T[j]) chmin(DP[i+1][j+1],DP[i][j]);
                else chmin(DP[i+1][j+1],DP[i][j]+1);

            }else if(S_info[i]==1 and T_info[j]==0){ //? t
                chmin(DP[i+1][j],DP[i][j]); //?なのでマッチせずiだけ進める
                if(S[i]==T[j]) chmin(DP[i+1][j+1],DP[i][j]);
                else chmin(DP[i+1][j+1],DP[i][j]+1);
            }else if(S_info[i]==0 and T_info[j]==1){ //s ?
                chmin(DP[i][j+1],DP[i][j]); //?なのでマッチせずjだけ進める
                if(S[i]==T[j]) chmin(DP[i+1][j+1],DP[i][j]);
                else chmin(DP[i+1][j+1],DP[i][j]+1);
            }else if(S_info[i]==2 and T_info[j]==0){ //* t
                chmin(DP[i+1][j],DP[i][j]); //*なのでマッチせずiだけ進める
                if(S[i]==T[j]) chmin(DP[i][j+1],DP[i][j]), chmin(DP[i+1][j+1],DP[i][j]); //jだけ進めるので*を次もまた使える
                else chmin(DP[i][j+1],DP[i][j]+1), chmin(DP[i+1][j+1],DP[i][j]+1); //jだけ進めるので*を次もまた使える

            }else if(S_info[i]==0 and T_info[j]==2){ //s *
                chmin(DP[i][j+1],DP[i][j]); //*なのでマッチせずjだけ進める
                if(S[i]==T[j]) chmin(DP[i+1][j],DP[i][j]), chmin(DP[i+1][j+1],DP[i][j]); //iだけ進めるので*を次もまた使える
                else chmin(DP[i+1][j], DP[i][j]+1), chmin(DP[i+1][j+1],DP[i][j]+1);
            }else{ //マーク マーク
                chmin(DP[i+1][j],DP[i][j]);
                chmin(DP[i][j+1],DP[i][j]);
                chmin(DP[i+1][j+1],DP[i][j]);
            }
        }
    }
    REP(i,N) chmin(DP[i+1][M],DP[i][M]+ (S_info[i]==0));
    REP(j,M) chmin(DP[N][j+1],DP[N][j]+ (T_info[j]==0));
    auto ans = DP.back().back();
    cout << ans << endl;
}
0