Codeforces Round #425 (Div. 2) Problem B Petya and Exam (Codeforces 832B) - 暴力

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个可以接受的字符,'*'可以匹配空串或者全是不可接受的字符的字符串,但是至多出现一次。有一些询问,输出每个询问的字符串是否和模板串匹配。

  暴力就好。先判断长度(如果有’*‘另当别论),然后在进行匹配。'*'匹配的长度是可以计算出来的。总之暴力就好。

  因为有长度特判,所以它是卡不掉你的,最坏的情况下,时间复杂度为O(n1.5)。

Code

  1 /**
  2  * Codeforces
  3  * Problem#831B
  4  * Accepted
  5  * Time:31ms
  6  * Memory:2200k
  7  */
  8 #include <iostream>
  9 #include <cstdio>
 10 #include <ctime>
 11 #include <cmath>
 12 #include <cctype>
 13 #include <cstring>
 14 #include <cstdlib>
 15 #include <fstream>
 16 #include <sstream>
 17 #include <algorithm>
 18 #include <map>
 19 #include <set>
 20 #include <stack>
 21 #include <queue>
 22 #include <vector>
 23 #include <stack>
 24 #ifndef WIN32
 25 #define Auto "%lld"
 26 #else
 27 #define Auto "%I64d"
 28 #endif
 29 using namespace std;
 30 typedef bool boolean;
 31 const signed int inf = (signed)((1u << 31) - 1);
 32 const signed long long llf = (signed long long)((1ull << 61) - 1);
 33 const double eps = 1e-6;
 34 const int binary_limit = 128;
 35 #define smin(a, b) a = min(a, b)
 36 #define smax(a, b) a = max(a, b)
 37 #define max3(a, b, c) max(a, max(b, c))
 38 #define min3(a, b, c) min(a, min(b, c))
 39 template<typename T>
 40 inline boolean readInteger(T& u){
 41     char x;
 42     int aFlag = 1;
 43     while(!isdigit((x = getchar())) && x != '-' && x != -1);
 44     if(x == -1) {
 45         ungetc(x, stdin);    
 46         return false;
 47     }
 48     if(x == '-'){
 49         x = getchar();
 50         aFlag = -1;
 51     }
 52     for(u = x - '0'; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - '0');
 53     ungetc(x, stdin);
 54     u *= aFlag;
 55     return true;
 56 }
 57 
 58 int n;
 59 boolean charset[256];
 60 boolean hasxing = false;
 61 char S[100005];
 62 char T[100005];
 63 int lenS, lenT;
 64 
 65 inline void init() {
 66     gets(S);
 67     int len = strlen(S);
 68     memset(charset, false, sizeof(charset));
 69     for(int i = 0; i < len; i++)
 70         charset[S[i]] = true;
 71     gets(T); 
 72     lenT = strlen(T);
 73     for(int i = 0; i < lenT; i++)
 74         if(T[i] == '*') {
 75             hasxing = 1;
 76             break;
 77         }
 78 }
 79 
 80 boolean check() {
 81     lenS = strlen(S);
 82     if(lenT - hasxing > lenS)    return false;
 83     if(!hasxing && lenT != lenS)    return false;
 84     int i = 0, j = 0;
 85     while(i < lenT && j < lenS) {
 86         if(T[i] == '?') {
 87             if(!charset[S[j]])
 88                 return false;
 89             i++, j++;
 90         } else if(T[i] == '*') {
 91             int cnt = lenS - lenT + 1;
 92             for(int p = 1; p <= cnt; p++, j++) {
 93                 if(charset[S[j]])
 94                     return false;
 95             }
 96             i++;
 97         } else {
 98             if(T[i] != S[j])
 99                 return false;
100             i++, j++;
101         }
102     }
103     return true;
104 }
105 
106 inline void solve() {
107     readInteger(n);
108     gets(S);
109     while(n--) {
110         gets(S);
111         if(check())
112             puts("YES");
113         else
114             puts("NO");
115     }
116 }
117 
118 int main() {
119     init();
120     solve();
121     return 0;
122 }
posted @ 2017-07-25 17:59 阿波罗2003 阅读(...) 评论(...) 编辑 收藏