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

 

题意就是:下面的字符串匹配给出的标准的字符串,标准的字符串中如果是'?',就找“好的”字符串中的一个,匹配一下。如果是'*',就是“好的”字符串的补集或者为空。

本来想的是如果有'*',就把'*'之前的匹配一下,再从后面匹配一下,剩下的特殊判断就可以。

但是太菜,写的代码太长啦,还老是写错,看了一下题解,可以直接依次判断匹配下去。

 

贴一下大佬代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N=1e5+10;
 4 char a[N],b[N],flag[N];
 5 int main(){
 6     int n;
 7     memset(a,0,sizeof(a));
 8     memset(b,0,sizeof(b));
 9     memset(flag,0,sizeof(flag));
10     while(~scanf("%s",a)){
11         int len1=strlen(a);
12         for(int i=0;i<len1;i++)
13             flag[a[i]]=1;
14         scanf("%s",a);
15         len1=strlen(a);
16         scanf("%d",&n);
17         while(n--){
18             memset(b,0,sizeof(b));
19             scanf("%s",b);
20             int len2=strlen(b);
21             int i=0,j=0;
22             int cnt=0,ret=0;
23             while(i<len2){
24                 if(a[j]=='*'){
25                     for(int h=0;h<len2-len1+1;h++){  //直接特殊判断
26                         if(flag[b[i]]==1){cnt=1;break;} 
27                         i++;
28                     }
29                     j++;
30                     continue;
31                 }
32                 if(i>=len2)break;
33                 i++;j++;
34                 if((a[j-1]=='?'&&flag[b[i-1]]==1)||a[j-1]==b[i-1]) //匹配'?'和字母直接匹配
35                     continue;
36                 ret=1;    //如果走到这一步,说明上面匹配的有不符合条件的
37                 if(ret)break;
38             }
39             if(j<len1&&(len1-len2>1||a[len1-1]!='*'))  //如果标准串比要匹配的串的长度>1,说明不是完全匹配
40             if(ret)printf("NO\n");
41             else printf("YES\n");
42         }
43     }
44     return 0;
45 }

 

 

 

太菜啦,加油啦(╯°Д°)╯︵┻━┻

 

 

 

posted @ 2017-11-07 19:42  ZERO-  阅读(564)  评论(0编辑  收藏  举报