結果

問題 No.874 正規表現間距離
ユーザー 👑 kmjpkmjp
提出日時 2019-08-30 22:18:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 79 ms / 2,000 ms
コード長 1,949 bytes
コンパイル時間 1,608 ms
コンパイル使用メモリ 172,668 KB
実行使用メモリ 67,456 KB
最終ジャッジ日時 2024-05-01 18:29:58
合計ジャッジ時間 4,746 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
67,076 KB
testcase_01 AC 44 ms
67,200 KB
testcase_02 AC 44 ms
67,072 KB
testcase_03 AC 45 ms
67,072 KB
testcase_04 AC 46 ms
67,200 KB
testcase_05 AC 47 ms
67,072 KB
testcase_06 AC 25 ms
67,072 KB
testcase_07 AC 24 ms
67,200 KB
testcase_08 AC 27 ms
67,152 KB
testcase_09 AC 24 ms
67,072 KB
testcase_10 AC 26 ms
67,072 KB
testcase_11 AC 23 ms
67,072 KB
testcase_12 AC 26 ms
67,072 KB
testcase_13 AC 23 ms
67,072 KB
testcase_14 AC 28 ms
67,072 KB
testcase_15 AC 25 ms
67,072 KB
testcase_16 AC 32 ms
67,328 KB
testcase_17 AC 37 ms
67,312 KB
testcase_18 AC 27 ms
67,040 KB
testcase_19 AC 23 ms
67,072 KB
testcase_20 AC 26 ms
67,072 KB
testcase_21 AC 24 ms
67,180 KB
testcase_22 AC 26 ms
67,072 KB
testcase_23 AC 24 ms
67,072 KB
testcase_24 AC 45 ms
67,072 KB
testcase_25 AC 43 ms
67,316 KB
testcase_26 AC 45 ms
67,456 KB
testcase_27 AC 75 ms
67,456 KB
testcase_28 AC 67 ms
67,456 KB
testcase_29 AC 76 ms
67,412 KB
testcase_30 AC 79 ms
67,348 KB
testcase_31 AC 74 ms
67,412 KB
testcase_32 AC 75 ms
67,328 KB
testcase_33 AC 24 ms
67,456 KB
testcase_34 AC 27 ms
67,072 KB
testcase_35 AC 24 ms
67,100 KB
testcase_36 AC 39 ms
67,280 KB
testcase_37 AC 36 ms
67,200 KB
testcase_38 AC 26 ms
67,208 KB
testcase_39 AC 23 ms
67,072 KB
testcase_40 AC 27 ms
67,072 KB
testcase_41 AC 23 ms
67,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

string A,B;
vector<pair<char,int>> X,Y;
int N,M;
int memo[4040][4040];

vector<pair<char,int>> conv(string S) {
	vector<pair<char,int>> R;
	int i=0;
	while(i<S.size()) {
		if(i+1<S.size()&&S[i+1]=='?') {
			R.push_back({S[i],0});
			i+=2;
		}
		else if(i+1<S.size()&&S[i+1]=='*') {
			R.push_back({S[i],2});
			i+=2;
		}
		else {
			R.push_back({S[i],1});
			i++;
		}
	}
	//FORR(r,R) cout<<(char)r.first<<r.second<<" ";
	//cout<<endl;
	return R;
}

int hoge(int x,int y) {
	if(memo[x][y]>=0) return memo[x][y];
	if(x==N&&y==M) return 0;
	int ret=1<<30;
	
	if(x==N) {
		ret=(Y[y].second==1)+hoge(x,y+1);
	}
	else if(y==M) {
		ret=(X[x].second==1)+hoge(x+1,y);
	}
	else {
		ret=min(ret,1+hoge(x+1,y+1));
		ret=min(ret,1+hoge(x+1,y));
		ret=min(ret,1+hoge(x,y+1));
		if(X[x].second==0||X[x].second==2) ret=min(ret,hoge(x+1,y));
		if(Y[y].second==0||Y[y].second==2) ret=min(ret,hoge(x,y+1));
		
		if(X[x].first==Y[y].first) {
			ret=min(ret,hoge(x+1,y+1));
			if(Y[y].second==2) ret=min(ret,hoge(x+1,y));
			if(X[x].second==2) ret=min(ret,hoge(x,y+1));
		}
		
		
	}
	//cout<<x<<y<<" "<<ret<<endl;
	return memo[x][y]=ret;
	
	
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>A>>B;
	X=conv(A);
	Y=conv(B);
	N=X.size();
	M=Y.size();
	
	MINUS(memo);
	cout<<hoge(0,0)<<endl;
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0