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 << ) - );
const signed long long llf = (signed long long)((1ull << ) - );
const double eps = 1e-;
const int binary_limit = ;
#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 = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) {
ungetc(x, stdin);
return false;
}
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
return true;
} int n;
boolean charset[];
boolean hasxing = false;
char S[];
char T[];
int lenS, lenT; inline void init() {
gets(S);
int len = strlen(S);
memset(charset, false, sizeof(charset));
for(int i = ; i < len; i++)
charset[S[i]] = true;
gets(T);
lenT = strlen(T);
for(int i = ; i < lenT; i++)
if(T[i] == '*') {
hasxing = ;
break;
}
} boolean check() {
lenS = strlen(S);
if(lenT - hasxing > lenS) return false;
if(!hasxing && lenT != lenS) return false;
int i = , j = ;
while(i < lenT && j < lenS) {
if(T[i] == '?') {
if(!charset[S[j]])
return false;
i++, j++;
} else if(T[i] == '*') {
int cnt = lenS - lenT + ;
for(int p = ; 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 ;
}

Codeforces Round #425 (Div. 2) Problem B Petya and Exam (Codeforces 832B) - 暴力的更多相关文章

  1. Codeforces Round #425 (Div. 2) Problem A Sasha and Sticks (Codeforces 832A)

    It's one more school day now. Sasha doesn't like classes and is always bored at them. So, each day h ...

  2. 【Codeforces Round #425 (Div. 2) B】Petya and Exam

    [Link]:http://codeforces.com/contest/832/problem/B [Description] *能代替一个字符串(由坏字母组成); ?能代替单个字符(由好字母组成) ...

  3. Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状数组

    Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations ...

  4. Codeforces Round #425 (Div. 2) Problem C Strange Radiation (Codeforces 832C) - 二分答案 - 数论

    n people are standing on a coordinate axis in points with positive integer coordinates strictly less ...

  5. Codeforces Round #716 (Div. 2), problem: (B) AND 0, Sum Big位运算思维

    & -- 位运算之一,有0则0 原题链接 Problem - 1514B - Codeforces 题目 Example input 2 2 2 100000 20 output 4 2267 ...

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

    题目链接:http://codeforces.com/contest/832/problem/B B. Petya and Exam time limit per test 2 seconds mem ...

  7. Codeforces Round #425 (Div. 2) B - Petya and Exam

    地址:http://codeforces.com/contest/832/problem/B 题目: B. Petya and Exam time limit per test 2 seconds m ...

  8. Codeforces Round #425 (Div. 2))——A题&&B题&&D题

    A. Sasha and Sticks 题目链接:http://codeforces.com/contest/832/problem/A 题目意思:n个棍,双方每次取k个,取得多次数的人获胜,Sash ...

  9. Codeforces Round #753 (Div. 3), problem: (D) Blue-Red Permutation

    还是看大佬的题解吧 CFRound#753(Div.3)A-E(后面的今天明天之内补) - 知乎 (zhihu.com) 传送门  Problem - D - Codeforces 题意 n个数字,n ...

随机推荐

  1. robot framework自定义python库

    自定义python库的好处: robot framework填表式,将python的灵活性弄没了,但是不要担心,RF早就想到了解决办法,就是扩充自己的库. 1.在python应用程序包目录下创建一个新 ...

  2. OEMCC 13.2 集群版本安装部署

    之前测试部署过OEMCC 13.2单机,具体可参考之前随笔: OEMCC 13.2 安装部署 当时环境:两台主机,系统RHEL 6.5,分别部署OMS和OMR: OMS,也就是OEMCC的服务端 IP ...

  3. NewWord

    identification: 鉴定,识别; 验明; 身份证明; 认同; peer:PEER-TO-PEER:同等延迟机制.根据网络中共享资源方式的不同,局域网有两种组织形式 filters: n. ...

  4. 35.HTML--网页自动跳转 5种方法

    网页自动跳转 5种方法 利用HTML标记(META中的REFRESH属性) 用HTML标记,就是用META的REFRESH标记,举例如下: <meta http-equiv=refresh co ...

  5. oracle 常用(一)

    常用的6个分组函数: 注意点:where字句中不能使用组函数.要用到having函数. 但是从优化角度看,尽量使用where avg :平均值           sum:求和            ...

  6. ecshop 前台个人中心修改侧边栏 和 侧边栏显示不全 或 导航现实不全

    怎么给个人中心侧边栏加项或者减项 在模板文件default/user_menu.lbi 文件里添加或者修改,一般看到页面都会知道怎么加,怎么删,这里就不啰嗦了 添加一个栏目以后,这个地址跳的页面怎么写 ...

  7. gitlab数据迁移至其他gitlb服务器上

    需求: A : 待迁移服务器,上边存有数据 B:接收项目得服务器,本身存在数据 验证方案: 一,搭建gitlab8.15.2 OS:rhel7.4 yum install policycoreutil ...

  8. 城市里的间谍B901

    城市里的间谍   城市里的间谍 难度级别:C: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 某城市的地铁是线性的,有 n(2 <= n ...

  9. 什么是 shell

     shell 在计算机科学中,Shell俗称壳(用来区别于核),是指“为使用者提供操作界面”的软件(命令解析器).它类似于DOS下的command.com和后来的cmd.exe.它接收用户命令,然后调 ...

  10. SQL SERVER镜像配置(包含见证服务器)

    镜像简介   重要说明:保持数据库镜像运行.如果您关闭数据库镜像,则必须执行完全备份并还原数据库以重建数据库镜像.   一. 简介 SQL SERVER 2005镜像基于日志同步,可良好实现故障转移. ...