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 <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define ios() ios::sync_with_stdio(false)
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
int n,i,j,al,bl,y;
string a,b,c(,);
int main()
{
cin>>a;
for(int k=;k<a.size();k++)
{
c[a[k]]=;
}
cin>>a;al=a.size();
cin>>n;
while(n--)
{
cin>>b;bl=b.size();y=;
for(i=j=;y && i<al;i++)//没有*模式串匹配穿必须长度相等。
{
if(a[i]=='*')//有*模式串要大于主串串长减一才行,可以匹配为空
{
while(j<bl-(al-i-))
if(c[b[j++]]) y=;
}
else if(a[i]=='?'?c[b[j]]:a[i]==b[j])
j<bl?j++:y=;
else y=;
}
if(j<bl) y=;
puts(y?"YES":"NO");
}
return ;
}

最新文章

  1. popupwindow展示
  2. ML 基础知识
  3. 利用IIS应用请求转发ARR实现IIS和tomcat整合共用80端口
  4. Sphinx 配置文件的说明【备忘】
  5. 证书与keytool
  6. Odoo Auto Backup Database And Set Linux task schedualer
  7. codeforces 680A A. Bear and Five Cards(水题)
  8. poj3207 Ikki&rsquo;s Story IV &ndash; Panda&rsquo;s Trick
  9. .NET清楚Cookies
  10. String类与Date类的转换
  11. tomcat不用工程名访问怎么配置?
  12. 穿透的 div ( pointer-events )
  13. 字符串:&quot;2016-09-21T18:57:50+08:00[Asia/Chungking]&quot; 转Date
  14. linux命令-sed,uniq,cut,wc
  15. 第三方app抽奖发送微信红包
  16. wiki leaks file link url
  17. CF786B Legacy
  18. Solr使用访问地址控制索引的,删除、创建
  19. 第43节:Java学前要点
  20. Python3学习笔记27-ConfigParser模块

热门文章

  1. Jeff Dean专访,有不少干货
  2. 关于App程序猿泡沫
  3. HDUOJ Let the Balloon Rise 1004
  4. IOC DI 专题
  5. Linux系统病毒防治
  6. java读取jpg图片旋转按比例缩放
  7. VC++中的延时函数
  8. 学习《概率机器人》中英文PDF+Probabilistic Robotics
  9. tac---反转输出文件
  10. php自定义加密和解密