[Codeforces] Round #425 Div.2 B. Petya and Exam

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

Analysis

这次还是掉Rating(因为本来只做AB两道,但这道炸了),哈哈哈哈现在变成绿名了。

A题签到题,就不说了。(如果我A认真点早点打完可能就不会掉Rating了QwQ)

B题题意:先输入两行,第一行为Good words(被选中的字母),第二行为字符串格式。之后为一个整数n且Following with n lines contains一堆需要你判断的字符串。

怎么判断呢?

第二行的字符串会是这样的格式:i?schorolop*?cute?

其中?可以替换成Good words中的一个字母,*可以被替换成不含有Good words的字符串(可以是空串)(PS:这是不是有点熟悉?),*不超过一个。

这题真的想跳过,因为我字符串太渣。然而看到“*”只有一个的限定我就勉强来一波暴力吧。

思路是这样,如果询问字符串长度和格式不一样,一般是*问题,其他部分都可以一一匹配。所以从字符串两端往中间的*扫描,如果在遇到 * 之前出现不匹配,就记为NO;

针对星号对应的字符串要另外处理。如果扫描过程中出现Good words,同样记为NO。

还有特判问题。我重构之后被Hack了两次= =。检查是否有星号,针对字符串长度进行检查,具体内容代码解释的比我更清楚嗯。

Code

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #define maxn 1000010
 5 using namespace std;
 6 
 7 int n,lenpat,lenask,p1,p2,star = 0;
 8 char a[maxn],pat[maxn],ask[maxn];
 9 bool gw[30],cont = true;
10 
11 int main(){
12     scanf("%s%s",a,pat);
13     
14     for(int i = 0;a[i];i++){
15         gw[a[i]-'a'] = true;
16     }
17     
18     for(int i = 0;pat[i];i++){
19         if(pat[i] == '*') star = 1;
20     }
21     
22     lenpat = strlen(pat);
23     
24     scanf("%d",&n);
25     
26     while(n--){
27         scanf("%s",ask);
28         
29         lenask = strlen(ask);
30         
31         cont = true;p1 = p2 = -1;
32         
33         if(lenask < lenpat-star) cont = false;
34         
35         if(!star && lenask != lenpat) cont = false;
36         
37         for(int i = 0;pat[i] && cont;i++){
38             if(pat[i] == '*'){
39                 p1 = i;
40                 break;
41             }else if(pat[i] == '?'){
42                 if(!gw[ask[i]-'a'])
43                     cont = false;
44             }else if(pat[i] != ask[i]) cont = false;
45         }
46         
47         for(int i = 1;i <= lenpat && cont;i++){
48             if(pat[lenpat-i] == '*'){
49                 p2 = lenask-i;
50                 break;
51             }else if(pat[lenpat-i] == '?'){
52                 if(!gw[ask[lenask-i]-'a'])
53                     cont = false;
54             }else if(pat[lenpat-i] != ask[lenask-i]) cont = false;
55         }
56         
57         for(int i = p1;i <= p2 && cont && p1 >= 0 && p2 >= 0;i++){
58             if(gw[ask[i]-'a']) cont = false;
59         }
60         
61         if(cont) printf("YES
");
62         else printf("NO
");
63     }
64     
65     return 0;
66 }
推荐不看
转载请注明出处 -- 如有意见欢迎评论
原文地址:https://www.cnblogs.com/Chorolop/p/7235908.html