Round #425 B. Petya and Exam

It's hard times now. Today Petya needs to score 100 points on Informatics exam. The tasks seem easy to Petya, but he thinks he lacks time to finish them all, so he asks you to help with one..

There is a glob pattern in the statements (a string consisting of lowercase English letters, characters "?" and "*"). It is known that character "*" occurs no more than once in the pattern.

Also, n query strings are given, it is required to determine for each of them if the pattern matches it or not.

Everything seemed easy to Petya, but then he discovered that the special pattern characters differ from their usual meaning.

A pattern matches a string if it is possible to replace each character "?" with one good lowercase English letter, and the character "*" (if there is one) with any, including empty, string of bad lowercase English letters, so that the resulting string is the same as the given string.

The good letters are given to Petya. All the others are bad.

Input

The first line contains a string with length from 1 to 26 consisting of distinct lowercase English letters. These letters are good letters, all the others are bad.

The second line contains the pattern — a string s of lowercase English letters, characters "?" and "*" (1 ≤ |s| ≤ 105). It is guaranteed that character "*" occurs in s no more than once.

The third line contains integer n (1 ≤ n ≤ 105) — the number of query strings.

n lines follow, each of them contains single non-empty string consisting of lowercase English letters — a query string.

It is guaranteed that the total length of all query strings is not greater than 105.

Output

Print n lines: in the i-th of them print "YES" if the pattern matches the i-th query string, and "NO" otherwise.

You can choose the case (lower or upper) for each letter arbitrary.

Examples
input
ab
a?a
2
aaa
aab
output
YES
NO
input
abc
a?a?a*
4
abacaba
abaca
apapa
aaaaax
output
NO
YES
NO
YES
Note

In the first example we can replace "?" with good letters "a" and "b", so we can see that the answer for the first query is "YES", and the answer for the second query is "NO", because we can't match the third letter.

Explanation of the second example.

  • The first query: "NO", because character "*" can be replaced with a string of bad letters only, but the only way to match the query string is to replace it with the string "ba", in which both letters are good.
  • The second query: "YES", because characters "?" can be replaced with corresponding good letters, and character "*" can be replaced with empty string, and the strings will coincide.
  • The third query: "NO", because characters "?" can't be replaced with bad letters.
  • The fourth query: "YES", because characters "?" can be replaced with good letters "a", and character "*" can be replaced with a string of bad letters "x".

 

 

 1 /*
 2  *
 3 ��Ŀ����˼���ȸ���һ���ַ�����ʾ����ĸ�����������Ա�ʾ�κκ���ĸ��
 4 ��������ʾ�κβ�����ĸ�Ĵ�����ÿ�β�ѯ�Ƿ����ԭ�ַ����õ�
 5 ˼·���ȴ�������ȵIJ���ٸ���*��λ�����жϡ�
 6 ��Ϊ���Ȳ�ȷ���ˣ�*�������ٸ��ַ�Ҳ......
 7 */
 8 #include <iostream>
 9 #include <stdio.h>
10 #include <string.h>
11 using namespace std;
12 
13 #define LL long long
14 #define mem(a,b) memset(a,b,sizeof a)
15 const int INF = 0x3f3f3f3f;
16 #define MAXN 100100
17 
18 
19 char a[100005];
20 char b[100005];
21 int fl[1000];
22 
23 int main(){
24     int n;
25     while(~scanf("%s",a)){
26         mem(fl,0);
27         int k1=strlen(a);
28         for(int i=0; i<k1; i++)
29             fl[a[i]]=1;    //distinct lowercase
30         scanf("%s",a);
31         k1=strlen(a);
32         scanf("%d",&n);
33         while(n--){
34             scanf("%s",b);
35             int k2=strlen(b);
36             int flag=0;
37             int i=0,j=0;
38             while(j<k2){
39                 if(a[i]=='*'){
40                     for(int q=0; q<k2-k1+1; q++){
41                         if(fl[b[j]]==1){
42                             flag=1;
43                             break;
44                         }
45                         j++;
46                     }
47                     i++;
48                     continue;
49                 }
50                 if(j>=k2) break;
51                 i++,j++;
52                 if((a[i-1]=='?'&&fl[b[j-1]]==1)||a[i-1]==b[j-1]) continue;
53                 flag=1;
54                 if(flag) break;
55             }
56             if(i<k1&&(k1-k2>1||a[k1-1]!='*')) flag=1;
57              //character "*" can be replaced with empty string
58             if(flag) printf("NO\n");
59             else printf("YES\n");
60         }
61     }
62     return 0;
63 }
64 
65 /*
66   ����
67 !��ȡ���IJ�����
68 
69 if(!iflg)
70 �������iflg��0����ô!iflag���Ƿ�0����ִ��{}�еĴ��롣
71 
72 ���iflg�Ƿ�0����ô!iflag����0���Ͳ���ִ��if�µ�{}�еĴ��롣
73 */

 

posted @ 2017-08-08 17:30  浅忆~  阅读(142)  评论(0编辑  收藏  举报