首页 > ACM题库 > HDU-杭电 > HDU 3901-Wildcard-KMP-[解题报告]HOJ
2015
04-14

HDU 3901-Wildcard-KMP-[解题报告]HOJ

Wildcard

问题描述 :

When specifying file names (or paths) in DOS, Microsoft Windows and Unix-like operating systems, the asterisk character (“*") substitutes for any zero or more characters, and the question mark (“?") substitutes for any one character.
Now give you a text and a pattern, you should judge whether the pattern matches the text or not.

输入:

There are several cases. For each case, only two lines. The first line contains a text, which contains only lower letters. The last line contains a pattern, which consists of lower letters and the two wildcards (“*", "?").
The text is a non-empty string and its size is less than 100,000, so do the pattern.

We ensure the number of “?” and the number of “*” in the pattern are both no more than 10.

输出:

There are several cases. For each case, only two lines. The first line contains a text, which contains only lower letters. The last line contains a pattern, which consists of lower letters and the two wildcards (“*", "?").
The text is a non-empty string and its size is less than 100,000, so do the pattern.

We ensure the number of “?” and the number of “*” in the pattern are both no more than 10.

样例输入:

abcdef
a*c*f

样例输出:

YES

 http://acm.hdu.edu.cn/showproblem.php?pid=3901

多校2011 (7)

 

题意:给定一个text串,和一个带*和?的模式串,看是否能匹配(题目说的匹配,但其实只是子串匹配就能过,标程是子串匹配的?,坑爹啊。。。只要是子串就可以了,开始以为一定要全匹配。。。),长度100000

*可以代表0或多个字符

?一定代表1个字符

 

分析:

对子串匹配:

对?而言,保证只要是?的地方对任何字符都匹配。。所以只要改动next函数和匹配函数里面,保证出线?都匹配即可。。。对*而言,相当于把原模式串分割成s1,s2,s3….对这些串依次去匹配,每次都返回了最靠前的匹配的位置,到最后如果不能匹配了说明无法匹配。。。

注意最后模式串可能剩一大堆***,判断一下下。。。

对全匹配:

待定。。。 

 

 

 

代码:

子串匹配

#include<iostream>
using namespace std;

const int N=100010;
char t[N], s[N], tmp[N];
int next[N], tn, sn, flag;

void getnext(char *s)
{
	int len=strlen(s);
	int i=0, j=-1;
	next[0] = -1;
	while(i<len)
	{
		if(j==-1 || s[i]==s[j] || s[i]=='?' || s[j]=='?')
			next[++i] = ++j;
		else
			j = next[j];
	}
}
int getindex(int ti, char tmp[])
{
	int len = strlen(tmp);
	int j=0;
	while(ti<tn && j<len)
	{
		if(j==-1 || t[ti]==tmp[j] || tmp[j]=='?')
		{
			ti++, j++;
		}
		else
			j = next[j];
	}
	if(j==len)
		return ti;
	return -1;
}

int main()
{
	int i, j, k, l;
	while(scanf("%s %s", t, s)!=EOF)
	{
		tn = strlen(t);
		sn = strlen(s);
		
		for(i=0,j=0; i<tn && j<sn; )
		{
			for( ; j<sn&&s[j]=='*'; j++); 
			for(k=j; k<sn&&s[k]!='*'; k++);

			for(l=j; l<k; l++)
				tmp[l-j] = s[l];
			tmp[l-j] = '\0';

			getnext(tmp);
			flag = getindex(i, tmp);
			if(flag==-1)
				break;
			i = flag;
			j = k;
		}
		while(j<sn && s[j]=='*')
			j++;
		if(j<sn)
			flag = -1;

		if(flag==-1)
			printf("NO\n");
		else
			printf("YES\n");
	}
	return 0;
}

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/ggggiqnypgjg/article/details/6659311


,