Round #425 B. Petya and Exam

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

aba?a2aaaaab

output

YESNO

input

abca?a?a*4abacabaabacaapapaaaaaax

output

NOYESNOYES

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 /*
 2  *
 3 ??????????????????????????????????????????????κκ??????
 4 ??????????κβ???????????????β?????????????????
 5 ?·????????????????????*??λ?????ж??
 6 ??????????????*?????????????......
 7 */
 8 #include <iostream>
 9 #include <stdio.h>
10 #include <string.h>
11 using namespace std;
12
13 #define LL long long
14 #define mem(a,b) memset(a,b,sizeof a)
15 const int INF = 0x3f3f3f3f;
16 #define MAXN 100100
17
18
19 char a[100005];
20 char b[100005];
21 int fl[1000];
22
23 int main(){
24     int n;
25     while(~scanf("%s",a)){
26         mem(fl,0);
27         int k1=strlen(a);
28         for(int i=0; i<k1; i++)
29             fl[a[i]]=1;    //distinct lowercase
30         scanf("%s",a);
31         k1=strlen(a);
32         scanf("%d",&n);
33         while(n--){
34             scanf("%s",b);
35             int k2=strlen(b);
36             int flag=0;
37             int i=0,j=0;
38             while(j<k2){
39                 if(a[i]==‘*‘){
40                     for(int q=0; q<k2-k1+1; q++){
41                         if(fl[b[j]]==1){
42                             flag=1;
43                             break;
44                         }
45                         j++;
46                     }
47                     i++;
48                     continue;
49                 }
50                 if(j>=k2) break;
51                 i++,j++;
52                 if((a[i-1]==‘?‘&&fl[b[j-1]]==1)||a[i-1]==b[j-1]) continue;
53                 flag=1;
54                 if(flag) break;
55             }
56             if(i<k1&&(k1-k2>1||a[k1-1]!=‘*‘)) flag=1;
57              //character "*" can be replaced with empty string
58             if(flag) printf("NO\n");
59             else printf("YES\n");
60         }
61     }
62     return 0;
63 }
64
65 /*
66   ????
67 !????????????
68
69 if(!iflg)
70 ???????iflg??0?????!iflag?????0???????{}?е????
71
72 ???iflg???0?????!iflag????0??????????if?μ?{}?е????
73 */
时间: 2024-10-10 07:43:56

Round #425 B. Petya and Exam的相关文章

Codeforces Round #425 B

Petya and Exam 题意:定义n个字符(小写字母)是好的,其余小写字母都是坏的,给一个字符s串含有"?"表示"?"可以替换成任意好的字符,含有最多一个"*"表示"*"可以替换成任意长度的由坏的字符组成的字符串,给q个询问,每个询问有一个小写字母组成的字符串,询问字符串经过替换后能否与查询的串一样 思路:xjb模拟,题意真jb迷 AC代码: #include "iostream" #include

Codeforces Round #425 (Div.2)

A.Sasha and Sticks 题意:有两个人,每次轮流从n个棒子中抽出k个,直到剩余不足k个棒子.Sasha先抽.如果Sasha抽取的多,则输出YES,否则输出NO. 思路:n/k为奇数时,Sasha获胜. 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 typedef long long LL; 5 LL n, k; 6 int main() 7 { 8 while (~scanf(&quo

Codeforces Round #425 (Div. 2) B. Petya and Exam(暴力大法好)

题目链接:http://codeforces.com/problemset/problem/832/B 题意:给定一些小写字符(定义为好字符,除这些好字符外的其他小写字符都是坏字符).和字符串S,S里面除了小写字母以外还有?字符,?可以用任何的好字符替代:*字符,*只可以用坏字符串和 空替代,然后给出n个字符串去匹配S字符,问是否能成功. 题解:QAQ,这道题写的我头发又掉了好多. 设拿来匹配S的字符串为T 字符串要匹配,长度要相同吧.*既然这么强大,可以空,那么T的长度最多比S的长度少1:然后

Codeforces_832B: Petya and Exam

题目链接 暴力模拟..好好读题就好了..羡慕英语好的 #include<bits/stdc++.h> using namespace std; typedef long long LL; bool vis[150]; string t; string p,p1; int q; bool ok(string t) { if(t.length()<p1.length()) return false; else { int l=1; //在i指针遇到'*'前,l无具体意义,只为抵消-1的影响:

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 connected with n - 1 routes so that each route connects two stations, and it is possible to reach every station from any other. The boys decided to h

Codeforces Round #425 D

Misha, Grisha and Underground 题意:给一颗树,每个点权值为1,q个询问,每个询问给出a,b,c,3 个点,选择一个点为起点,一个点为终点,形成一条路径,第3个点做为第二条路径的起点,问2条路径上重复区间的点权和的最大值 思路:树链剖分或者LCA,树链剖分映射到数状数组上,没有更新,求出ab ac bc之间的权值和,最长的路径和第二长的路径重复的部分就是答案 AC代码: #include "iostream" #include "string.h&

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

n people are standing on a coordinate axis in points with positive integer coordinates strictly less than 106. For each person we know in which direction (left or right) he is facing, and his maximum speed. You can put a bomb in some point with non-n

Codeforces Round #425 A

Sasha and Sticks 题意:n根棍子,每次拿走k根,当不足k根的时候不能拿,输,判断先手能否赢 思路:(n/k)&1,直接判断能经行奇数次取木棍还是偶数 AC代码: #include "iostream" #include "string.h" #include "stack" #include "queue" #include "string" #include "vecto

【树链剖分】【dfs序】【LCA】【分类讨论】Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground

一棵树,q次询问,每次给你三个点a b c,让你把它们选做s f t,问你把s到f +1后,询问f到t的和,然后可能的最大值是多少. 最无脑的想法是链剖线段树--但是会TLE. LCT一样无脑,但是少一个log,可以过. 正解是分类讨论, 如果t不在lca(s,f)的子树内,答案是dis(lca(s,f),f). 如果t在lca(s,f)的子树内,并且dep(lca(s,t))>dep(lca(f,t)),答案是dis(lca(s,t),f): 否则答案是dis(lca(f,t),f). #in