Hotel

Problem Description

Last year summer Max straveled to California for his vacation. He had a great time there: took many photos, visited famous universities, enjoyed beatiful beaches and tasted various delicius 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 names is. So Max searched in web for the information of hotels in California and 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 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

For each test data set, just simply output one integer in a line telling the number of hotel in the list whose name matches the one Max remembered.

Sample Input

herbert
2
amazon
herbert
?ert*
2
amazon
herbert
*
2
amazon
anything
hertber?
2
amazon
herber

Sample Output

1
1
2
0

 1 #include <stdio.h>
 2 #include <string.h>
 3 char s[10010][51];
 4 char t[51];
 5 
 6 int match(char *s,char *t,int st,int se,int m,int n)
 7 {
 8     int i,j,k;
 9     if(st == m && se == n) return 1;
10     i = st;
11     j = se;
12     while(i < m && j < n)
13     {
14         if(s[i] == t[j]||s[i]=='?')
15         {
16             i ++;
17             j ++;
18         }
19         else if(s[i]=='*')
20         {
21             for(k = j; k <= n; k ++)
22             {
23                 if(match(s,t,i+1,k,m,n)) return 1;
24             }
25             break;
26         }
27         else return 0;
28     }
29     if(i == m && j == n)
30         return 1;
31     return 0;
32 }
33 
34 int main()
35 {
36     int i,n,tt,m,ans,n1;
37     while(scanf("%s",t)!=EOF)
38     {
39         scanf("%d",&n);
40         for(i = 1; i <= n; i ++)
41             scanf("%s",s[i]);
42         ans = 0;
43         for(i = 1; i <= n; i ++)
44         {
45             m = strlen(s[i]);
46             n1 = strlen(t);
47             if(match(t,s[i],0,0,n1,m)) ans ++;
48         }
49         printf("%d\n",ans);
50     }
51 
52     return 0;
53 }
View Code

 

posted @ 2013-05-28 22:56  1002liu  阅读(160)  评论(0编辑  收藏  举报