Codeforces Round #425 (Div. 2) B. Petya and Exam(字符串模拟 水)

时间:2022-12-15 02:32:11

题目链接:http://codeforces.com/contest/832/problem/B

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

题目大意:

  给定一个字符串,都是'a'~'z'中的字符,我们定义为好字符。

  给定一个母串,并询问n个子串能否有正确结果符合如下:

    1.母串'?'可以替换成任何好字符

    2.'*'只能被一个或多个坏字符,或空字符替换。

解题思路

  简单字符串模拟,主要难在母串有'*'时候,分别比较'*'前后两部分是否符合1操作,

 再询问子串其他部分是否有坏字符即可。具体看代码吧- -

AC代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define For(I,A,B) for(int I=(A);I<(B);++I)
#define Rep(I,N) For(I,0,N)
#define mem(A,val) memset(A,val,sizeof(A))
const int maxn=1e5+;
int n,len1,len2,pos;
bool star=false,flag,vis[];
int main()
{
string a,b,str;
cin>>str>>a>>n;
mem(vis,false);
Rep(i,str.size())
vis[str[i]-'a']=true; //能替换的字符我们标记为好字符
Rep(i,a.size())
if(a[i]=='*') { star=true;pos=i;break; }
while(n--)
{
cin>>b;
flag=false;
len1=a.size(); len2=b.size();
if((star&&len1-len2>) || (!star&&len2!=len1))
{ puts("NO");continue; } //长度都不符合就直接NO了
if(!star)
{ //母串无'*'时候随便比较下就是了
for(int i=;i<len1&&!flag;i++)
{
if(a[i]==b[i] || (a[i]=='?'&&vis[b[i]-'a']))
continue;
else flag=true;
}
}
else
{ //母串有‘*’时
for(int i=;i<pos&&!flag;i++)
{ //‘*’前面的部分,母串子串按规则比较
if(a[i]==b[i] || (a[i]=='?'&&vis[b[i]-'a']))
continue;
else flag=true;
}
int k=len2-;
for(int j=len1-;j>pos;j--,k--)
{ // ‘*’后面的部分,母串子串依旧要按规则比较
if(b[k]==a[j]||(a[j]=='?'&&vis[b[k]-'a']))
continue;
else flag=true;
}
if(!flag)
{ //按题意,子串剩余部分,应该是一些坏字符
for(int i=pos;i<=k&&!flag;i++)
{
if(vis[b[i]-'a']) flag=true;
}
}
}
if(flag) puts("NO");
else puts("YES");
}
return ;
}