您的位置:首页 > 其它

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

2017-07-25 17:59 423 查看
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

/**
* Codeforces
* Problem#831B
* Accepted
* Time:31ms
* Memory:2200k
*/
#include <iostream>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <stack>
#ifndef WIN32
#define Auto "%lld"
#else
#define Auto "%I64d"
#endif
using namespace std;
typedef bool boolean;
const signed int inf = (signed)((1u << 31) - 1);
const signed long long llf = (signed long long)((1ull << 61) - 1);
const double eps = 1e-6;
const int binary_limit = 128;
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
template<typename T>
inline boolean readInteger(T& u){
char x;
int aFlag = 1;
while(!isdigit((x = getchar())) && x != '-' && x != -1);
if(x == -1) {
ungetc(x, stdin);
return false;
}
if(x == '-'){
x = getchar();
aFlag = -1;
}
for(u = x - '0'; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - '0');
ungetc(x, stdin);
u *= aFlag;
return true;
}

int n;
boolean charset[256];
boolean hasxing = false;
char S[100005];
char T[100005];
int lenS, lenT;

inline void init() {
gets(S);
int len = strlen(S);
memset(charset, false, sizeof(charset));
for(int i = 0; i < len; i++)
charset[S[i]] = true;
gets(T);
lenT = strlen(T);
for(int i = 0; i < lenT; i++)
if(T[i] == '*') {
hasxing = 1;
break;
}
}

boolean check() {
lenS = strlen(S);
if(lenT - hasxing > lenS)    return false;
if(!hasxing && lenT != lenS)    return false;
int i = 0, j = 0;
while(i < lenT && j < lenS) {
if(T[i] == '?') {
if(!charset[S[j]])
return false;
i++, j++;
} else if(T[i] == '*') {
int cnt = lenS - lenT + 1;
for(int p = 1; p <= cnt; p++, j++) {
if(charset[S[j]])
return false;
}
i++;
} else {
if(T[i] != S[j])
return false;
i++, j++;
}
}
return true;
}

inline void solve() {
readInteger(n);
gets(S);
while(n--) {
gets(S);
if(check())
puts("YES");
else
puts("NO");
}
}

int main() {
init();
solve();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: