CF-832B

B. Petya and Exam
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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".

 

题意:

首先给出的是good字母,第二行为标准句s,第三行为匹配串的个数n,其后跟n个串。

'?'可被替换为good字母,'*'可被替换为任意非good字符(包括空格)。

问充分运用规则后两串是否匹配。

 

AC代码:

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 
  4 const int maxn=1e5+10;
  5 int good[200];
  6 char s[maxn],p[maxn],a[30];
  7 
  8 int main(){
  9     int n,pos=-1;
 10     cin>>a;
 11     int len=strlen(a);
 12     for(int i=0;i<len;i++){
 13         good[a[i]]++;
 14     }
 15     cin>>s;
 16     len=strlen(s);
 17     for(int i=0;i<len;i++){
 18         if(s[i]=='*'){
 19             pos=i;
 20             break;
 21         }
 22     }
 23     cin>>n;
 24     while(n--){
 25         int flag=1;
 26         cin>>p;
 27         int lenp=strlen(p);
 28         if(pos==-1){
 29             if(len!=lenp){
 30                 cout<<"NO"<<endl;
 31                 flag=0;
 32                 continue;
 33             }
 34             for(int i=0;i<lenp;i++){
 35                 if(s[i]=='?'){
 36                     if(good[p[i]]!=1){
 37                         cout<<"NO"<<endl;
 38                         flag=0;
 39                         break;
 40                     }
 41                 }
 42                 else if(s[i]!=p[i]){
 43                     cout<<"NO"<<endl;
 44                     flag=0;
 45                     break;
 46                 }
 47             }
 48             if(flag){
 49                 cout<<"YES"<<endl;
 50             }
 51         }
 52         else{
 53             if(len-lenp>=2){
 54                 cout<<"NO"<<endl;
 55                 continue;
 56             }
 57             for(int i=0;i<pos;i++){
 58                 if(s[i]=='?'){
 59                     if(good[p[i]]!=1){
 60                         cout<<"NO"<<endl;
 61                         flag=0;
 62                         break;
 63                     } 
 64                 }
 65                 else if(s[i]!=p[i]){
 66                     cout<<"NO"<<endl;
 67                     flag=0;
 68                     break;
 69                 }
 70             }
 71             if(!flag)continue;
 72             int t=lenp-len+pos;//*替换的部分 
 73             for(int i=pos;i<=t;i++){
 74                 if(good[p[i]]!=0){
 75                     cout<<"NO"<<endl;
 76                     flag=0;
 77                     break;
 78                 }
 79             }
 80             if(!flag)continue;
 81             for(int i=len-1,j=lenp-1;i>pos;i--,j--){
 82                 if(s[i]=='?'){
 83                     if(good[p[j]]!=1){
 84                         cout<<"NO"<<endl;
 85                         flag=0;
 86                         break;
 87                     }
 88                 }
 89                 else if(s[i]!=p[j]){
 90                     cout<<"NO"<<endl;
 91                     flag=0;
 92                     break;
 93                 }
 94             }
 95             if(flag)
 96             cout<<"YES"<<endl;
 97         }
 98     }
 99     return 0;
100 } 

 

转载于:https://www.cnblogs.com/Kiven5197/p/7233016.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值