Last year summer Max traveled to California for his vacation. He had a great time there: took many photos, visited famous universities, enjoyed beautiful beaches and tasted various delicious foods. It is such a good trip that Max plans to travel there one more time this year. Max is satisfied with the accommodation of the hotel he booked last year but he lost the card of that hotel and can not remember quite clearly what its name is. So Max searched
in the web for the information of hotels in California ans got piles of choice. Could you help Max pick out those that might be the right hotel?
in the web for the information of hotels in California ans got piles of choice. Could you help Max pick out those that might be the right hotel?
Input
Input may consist of several test data sets. For each data set, it can be format as below: For the first line, there is one string consisting 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<=300)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.
In the next line there is one integer n(1<=n<=300)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
For each test set. just simply one integer in a line telling the number of hotel in the list whose matches the one Max remembered.
Sample Input
herbert 2 amazon herbert ?ert* 2 amazon herbert * 2 amazon anything herbert? 2 amazon herber
Sample Output
1 0 2 0
/*
题意:字符串的匹配,给出一个指定串,再给出多个串,求有几个串符合要求。其中的要求是
'*'可以与任何字符匹配且它可以代表的长度不限,可以是0,1... 。'?'的长度为1,但也能与
任意字符匹配。
*/
标程:
#include<stdio.h> #include<string.h> char a[55],b[55]; int len,len1; int dfs(int x,int y) { int i; for(; x<len && y<len1; x++,y++) { if(a[x]=='*') { for(i=0;i<=len1;i++) if(dfs(x+1,y+i)) return 1; return 0; } else if(a[x]!='?' && a[x]!=b[y]) return 0; } if(x!=len || y!=len1) return 0; else return 1; } int main() { //freopen("a.txt","r",stdin); int n,i; while(scanf("%s",a)!=EOF) { scanf("%d",&n); len=strlen(a); getchar(); int cnt=0; for(i=0;i<n;i++) { scanf("%s",b); len1=strlen(b); cnt+=dfs(0,0); } printf("%d\n",cnt); } return 0; }
//DP代码:
/*
#include<stdio.h> #include<string.h> int dp[60][60]; int main() { //freopen("a.txt","r",stdin); int i,j,n; char a[55],b[55]; while(scanf("%s",a)!=EOF) { int len1=strlen(a),k=0,len2; scanf("%d",&n); while(n--) { scanf("%s",b); memset(dp,0,sizeof(dp)); len2=strlen(b); dp[0][0]=1; for(i=1;i<=len1;i++) { for(j=0;j<=len2;j++) { if(a[i-1]=='*') { if(j==0) dp[i][j]=dp[i-1][j]; else if(dp[i][j-1] || dp[i-1][j]) dp[i][j]=1; } else if(j) { if(a[i-1]=='?') dp[i][j]=dp[i-1][j-1]; else if (a[i-1]==b[j-1]) dp[i][j]=dp[i-1][j-1]; } } } if(dp[len1][len2]) k++; } printf("%d\n",k); } return 0; }
*/