like匹配使用的通配符_使用动态编程(DP)的通配符匹配问题

like匹配使用的通配符

Problem: You are given two strings (one query and other pattern). You have to find whether the pattern matches to query or not. Print '1' if matches otherwise '0'. Some rules for matching:

问题:给您两个字符串(一个查询和另一种模式)。 您必须查找模式是否与查询匹配。 如果匹配则打印“ 1”,否则匹配“ 0”。 一些匹配规则:

  1. A "*" can match any substring (blank spaces also).

    “ *”可以匹配任何子字符串(也可以是空格)。

  2. A "?" can match any single character (excluding blank spaces).

    一个“?” 可以匹配任何单个字符(空格除外)。

Constraints: (length of pattern) * (length of query) < 100000000

约束:(模式的长度)*(查询的长度)<100000000

    SAMPLE INPUT - 1:
    absbsbs
    a*?bs
    SAMPLE OUTPUT -1 :
    1

    SAMPLE INPUT – 2:
    avdgdv
    a?g*
    SAMPLE OUTPUT – 2:
    0

Explanation of the problem:

问题说明:

For the sample Input1,

对于示例输入1,

'*' can be replaced with 'bsb' and '?' can be replaced with 's'. Thus, both pattern and query matches so the output is 1.

'*'可以替换为'bsb'和'?' 可以用“ s”代替。 因此,模式和查询都匹配,因此输出为1。

For the sample input2, 对于样本输入2,

'*' can be replaced with 'dv' but we cannot replace '?' with 'vd' as it can replace only one character. Thus, pattern and query cannot match so the output is 0.

'*'可以替换为'dv',但我们不能替换'?' 使用'vd',因为它只能替换一个字符。 因此,模式和查询无法匹配,因此输出为0。

Algorithm:

算法:

  1. STEP-1: Create a 2D dp matrix where jth cell of ith row represent whether the pattern (from ith index to the end) and the query (from jth index to end) matches or not.

    STEP-1:创建一个二维矩阵DP其中j i 小区的第i行表示是否该图案(从第i索引的端部)和查询(从 j 索引结束)的匹配或没有。

  2. STEP-2: If pattern is the empty and the query is non-empty then it does not matches. So initializing dp matrix with false for the cell corresponding to this situation.

    步骤2:如果pattern为空且查询为非空,则不匹配。 因此,对于与这种情况相对应的单元,用false初始化dp矩阵。

  3. STEP-3: if the query is empty and the pattern contains all '*' (pattern is something like ******) then put these corresponding values true.

    步骤3:如果查询为空,且模式包含所有'*' (模式类似于******),则将这些对应的值设为true。

  4. STEP-4: Start filling the dp matrix from right to left and bottom to top.

    步骤4:从右到左,从下到上开始填充dp矩阵。

  5. STEP-5: If the ith character of pattern matches with jth character of query or the ith character is '?' then we can simply remove that ith character from pattern and jth character from query and check the right-bottom box and put its value in our current cell.

    步骤5:如果模式的第i个字符与查询的第j个字符匹配,或者第i个字符为“?” 那么我们可以简单地从模式中删除第ith个字符,从查询中删除第j个字符,然后选中右下角的框并将其值放在当前单元格中。

  6. STEP-6: if ith character of the pattern is '*' then check the right adjacent box(this box corresponds to situation when we remove the jth character from query) , bottom box(this box corresponds to situation when we remove the ith character from pattern) and the right-bottom adjacent(this box corresponds to situation when we remove the jth character from query and remove ith character from the pattern) value. If any of them is true then put true in current cell.

    步骤6:如果模式的第i个字符为'*',则选中右边的相邻框(此框对应于从查询中删除第j个字符时的情况),底部框(此框对应于删除第i个字符时的情况从模式)和右下角相邻(此框对应于从查询中删除第j个字符并从模式中删除第ith个字符的情况)值。 如果其中任何一个为true,则在当前单元格中为true。

  7. STEP-7: Otherwise, set cells to false

    步骤7:否则,将单元格设置为false

  8. STEP-8: return the answer of i=0 and j=0 as they represent full query and pattern.

    步骤8:返回i = 0和j = 0的答案,因为它们代表完整的查询和模式。

The time complexity of the above code is O(length of pattern * length of query).

上面代码的时间复杂度为O(模式长度*查询长度)。

C++ program:

C ++程序:

#include <iostream>
using namespace std;

bool wildCard(string pattern, string s){
	bool dp[pattern.length() + 1][s.length() + 1] = {false};
	// Intialization of few values necessary for the dp relations
	// Case 1. pattern is a blank string
	// In this case we only have to initialize right-bottom 
	// corner value to true as pattern is also empty and s is also empty
	dp[pattern.length()][s.length()] = true;
	// Case 2. s is empty 
	for(int row = pattern.length() - 1;row >= 0;row--){
		// if pattern contains only stars and s is empty only then it is true
		if(pattern[row] == '*' && dp[row + 1][s.length()] == true){
			dp[row][s.length()] = true;
		}else{
			dp[row][s.length()] = false;
		}
	}
	// filling the dp matrix(right to left and bottom to top) 
	for(int row = pattern.length() -1;row >= 0;row--){
		for(int col = s.length() - 1;col>=0;col--){
			// if the characters of pattern and s is equal or patterns 
			// rowth character is a ? then just pick up rigth bottom value
			if((pattern[row] == s[col]) || pattern[row] == '?'){
				dp[row][col] = dp[row + 1][col + 1];
			}
			// if there is a star then take the || of the bottom 
			// adjacent, right adjacent and rigth-bottom value;
			else if(pattern[row] == '*'){
				dp[row][col] = (dp[row+1][col] || dp[row+1][col+1] || dp[row][col+1]);
			}
			// otherwise store false
			else{
				dp[row][col] = false;
			}
		}
	}
	return dp[0][0];  
}
// driver programme for the code
int main() {
	string s, pattern;
	cin >> s;
	cin >> pattern;
	cout << s << endl;
	cout << pattern << endl;
	cout<<wildCard(pattern, s);
	return 0;
}

Output

输出量

absbsbs
a*?bs
1


翻译自: https://www.includehelp.com/algorithms/wild-card-matching-problem-using-dynamic-programming.aspx

like匹配使用的通配符

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值