Hotel
Problem Description
Input
For the first line, there is one string consist of ‘*’, ‘?’ and ‘a’-‘z’ characters. This string represents the hotel name that Max can remember. The ‘*’ and ‘?’ is wildcard characters. ‘*’ matches zero or more lowercase character(s), and ‘?’ matches only one lowercase character.
In the next line there is one integer n (1<=n<=10000) representing the number of hotel Max found, and then n lines follow. Each line contains one string of lowercase character(s), the name of the hotel.
The length of every string doesn’t exceed 50.
Output
Sample Input
herbert 2 amazon herbert ?ert* 2 amazon hertert * 2 amazon anything hertber? 2 amazon herber
Sample Output
1 1 2 0
题意:给定一个起始字符串,(起始串中有*?字符,*可以匹配零个或者多个字符,?只能匹配一个字符)。判断起始串通给定的串是否可以相匹配
AC代码:
#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
string lmx[10000];
int p[60][60];
int main()
{
string s;
int n,i,j,k,n1,n2,cnt,flag;
while(cin>>s)
{
cin>>n;
cnt=0;
n1=s.length();
for(i=0;i<n;i++)
{
cin>>lmx[i];
flag=0;
n2=lmx[i].length();
memset(p,0,sizeof(p));
p[0][0]=1;
for(j=1;j<=n1;j++)
{
for(k=0;k<=n2;k++)
{
if(s[j-1]=='*')
{
if(k==0) p[j][k]=p[j-1][k];
else p[j][k]=p[j-1][k]|p[j][k-1];
}
else if(k)
{
if(s[j-1]=='?') p[j][k]=p[j-1][k-1];
else if(s[j-1]==lmx[i][k-1]) p[j][k]=p[j-1][k-1];
}
}
}
if(p[n1][n2]==1) cnt++;
}
printf("%d\n",cnt);
}
return 0;
}