結果

問題 No.874 正規表現間距離
ユーザー leaf_1415leaf_1415
提出日時 2019-09-02 22:04:15
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 36 ms / 2,000 ms
コード長 1,771 bytes
コンパイル時間 774 ms
コンパイル使用メモリ 56,768 KB
実行使用メモリ 19,140 KB
最終ジャッジ日時 2023-08-22 18:02:12
合計ジャッジ時間 4,976 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 6 ms
7,428 KB
testcase_17 AC 6 ms
7,432 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,384 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,384 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 17 ms
11,584 KB
testcase_26 AC 15 ms
11,616 KB
testcase_27 AC 35 ms
19,112 KB
testcase_28 AC 27 ms
17,988 KB
testcase_29 AC 35 ms
19,068 KB
testcase_30 AC 36 ms
19,140 KB
testcase_31 AC 34 ms
17,972 KB
testcase_32 AC 34 ms
17,708 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 12 ms
11,712 KB
testcase_37 AC 12 ms
11,792 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 1 ms
4,380 KB
testcase_41 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>

using namespace std;

string s, t;
string s1, s2, t1, t2;
int dp[2005][2005];

int main(void)
{
	cin >> s >> t;
	
	for(int i = 0; i < s.size(); i++){
		if(s[i] == '?' || s[i] == '*'){
			s1.erase(s1.end()-1);
			s1 += s[i];
		}
		else s1 += s[i], s2 += s[i];
	}
	for(int i = 0; i < t.size(); i++){
		if(t[i] == '?' || t[i] == '*'){
			t1.erase(t1.end()-1);
			t1 += t[i];
		}
		else t1 += t[i], t2 += t[i];
	}
	s = s1, t = t1;
	
	int S = s.size(), T = t.size();
	s = "#" + s, t = "#" + t;
	s2 = "#" + s2, t2 = "#" + t2;
	
	for(int i = 0; i <= S; i++){
		for(int j = 0; j <= T; j++){
			dp[i][j] = 1e9;
		}
	}
	dp[0][0] = 0;
	
	for(int i = 0; i <= S; i++){
		for(int j = 0; j <= T; j++){
			if(j == T){
				if(i == S) continue;
				if(s[i+1] == '?' || s[i+1] == '*') dp[i+1][j] = min(dp[i+1][j], dp[i][j]);
				else dp[i+1][j] = min(dp[i+1][j], dp[i][j]+1);
				continue;
			}
			if(i == S){
				if(t[j+1] == '?' || t[j+1] == '*') dp[i][j+1] = min(dp[i][j+1], dp[i][j]);
				else dp[i][j+1] = min(dp[i][j+1], dp[i][j]+1);
				continue;
			}
			
			char c = s[i+1], d = t[j+1];
			if(c == '?' || c == '*') c = s2[i+1];
			if(d == '?' || d == '*') d = t2[j+1];
			
			if(c == d){
				dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j]);
				if(s[i+1] == '*') dp[i][j+1] = min(dp[i][j+1], dp[i][j]);
				if(t[j+1] == '*') dp[i+1][j] = min(dp[i+1][j], dp[i][j]);
			}
			
			dp[i+1][j] = min(dp[i+1][j], dp[i][j]+1); //delete
			dp[i][j+1] = min(dp[i][j+1], dp[i][j]+1); //insert
			dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j]+1); //update
			
			if(s[i+1] == '?' || s[i+1] == '*') dp[i+1][j] = min(dp[i+1][j], dp[i][j]);
			if(t[j+1] == '?' || t[j+1] == '*') dp[i][j+1] = min(dp[i][j+1], dp[i][j]);
		}
	}
	cout << dp[S][T] << endl;
	
	return 0;
}
0