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

题意:

首先给出的是good字母,第二行为标准句s,第三行为匹配串的个数n,其后跟n个串。

'?'可被替换为good字母,'*'可被替换为任意非good字符(包括空格)。

问充分运用规则后两串是否匹配。

AC代码:

 #include<bits/stdc++.h>
using namespace std; const int maxn=1e5+;
int good[];
char s[maxn],p[maxn],a[]; int main(){
int n,pos=-;
cin>>a;
int len=strlen(a);
for(int i=;i<len;i++){
good[a[i]]++;
}
cin>>s;
len=strlen(s);
for(int i=;i<len;i++){
if(s[i]=='*'){
pos=i;
break;
}
}
cin>>n;
while(n--){
int flag=;
cin>>p;
int lenp=strlen(p);
if(pos==-){
if(len!=lenp){
cout<<"NO"<<endl;
flag=;
continue;
}
for(int i=;i<lenp;i++){
if(s[i]=='?'){
if(good[p[i]]!=){
cout<<"NO"<<endl;
flag=;
break;
}
}
else if(s[i]!=p[i]){
cout<<"NO"<<endl;
flag=;
break;
}
}
if(flag){
cout<<"YES"<<endl;
}
}
else{
if(len-lenp>=){
cout<<"NO"<<endl;
continue;
}
for(int i=;i<pos;i++){
if(s[i]=='?'){
if(good[p[i]]!=){
cout<<"NO"<<endl;
flag=;
break;
}
}
else if(s[i]!=p[i]){
cout<<"NO"<<endl;
flag=;
break;
}
}
if(!flag)continue;
int t=lenp-len+pos;//*替换的部分
for(int i=pos;i<=t;i++){
if(good[p[i]]!=){
cout<<"NO"<<endl;
flag=;
break;
}
}
if(!flag)continue;
for(int i=len-,j=lenp-;i>pos;i--,j--){
if(s[i]=='?'){
if(good[p[j]]!=){
cout<<"NO"<<endl;
flag=;
break;
}
}
else if(s[i]!=p[j]){
cout<<"NO"<<endl;
flag=;
break;
}
}
if(flag)
cout<<"YES"<<endl;
}
}
return ;
}

CF-832B的更多相关文章

  1. ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'

    凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...

  2. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  3. cf Round 613

    A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...

  4. ARC下OC对象和CF对象之间的桥接(bridge)

    在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...

  5. [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现

    1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...

  6. CF memsql Start[c]UP 2.0 A

    CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...

  7. CF memsql Start[c]UP 2.0 B

    CF memsql Start[c]UP 2.0 B B. Distributed Join time limit per test 1 second memory limit per test 25 ...

  8. CF #376 (Div. 2) C. dfs

    1.CF #376 (Div. 2)    C. Socks       dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...

  9. CF #375 (Div. 2) D. bfs

    1.CF #375 (Div. 2)  D. Lakes in Berland 2.总结:麻烦的bfs,但其实很水.. 3.题意:n*m的陆地与水泽,水泽在边界表示连通海洋.最后要剩k个湖,总要填掉多 ...

  10. CF #374 (Div. 2) D. 贪心,优先队列或set

    1.CF #374 (Div. 2)   D. Maxim and Array 2.总结:按绝对值最小贪心下去即可 3.题意:对n个数进行+x或-x的k次操作,要使操作之后的n个数乘积最小. (1)优 ...

随机推荐

  1. java对象访问

    下面这句代码: Object obj = new Object(); 对象引用在栈中,对象实体存在堆中,引用的方式有两种,分别是通过句柄访问对象和通过直接指针访问对象. Sun HotSpot使用第二 ...

  2. git操作演示

    阶段一: git init git config --global user.email "you@example.com" git config --global user.na ...

  3. 区分拖曳(drag)和点击(click)事件

    假设页面上有一个a标签: <a href="http://www.google.com">google</a> 现在需要对这个标签进行拖放操作,会发现当拖曳 ...

  4. Codeforces Round #243 (Div. 1)——Sereja and Squares

    题目链接 题意: 给n个点,求能组成的正方形的个数. 四边均平行与坐标轴 大神的分析: 经典题 我们考虑每一种x坐标,显然仅仅有<= sqrt{N}个x坐标出现了> sqrt{N}次,我们 ...

  5. return和exit的差别

    #include<stdio.h> #include<sys/types.h> #include<sys/wait.h> #include<unistd.h& ...

  6. LeetCode(83)题解: Remove Duplicates from Sorted List

    https://leetcode.com/problems/remove-duplicates-from-sorted-list/ 题目: Given a sorted linked list, de ...

  7. Mac 操作系统安装 SVN server教程(Subversion With Mac OS X Tutorial)

    Find recent articles on my github page: rubyrobot.github.io © 2006-2014 Imagine Ecommerce Subversion ...

  8. python网络爬虫之使用scrapy爬取图片

    在前面的章节中都介绍了scrapy如何爬取网页数据,今天介绍下如何爬取图片. 下载图片需要用到ImagesPipeline这个类,首先介绍下工作流程: 1 首先需要在一个爬虫中,获取到图片的url并存 ...

  9. VB.Command()的参数

    VB.Command()的参数的来源有三个地方 1配置文件2启动参数3框架

  10. HDU4549 M斐波那契数列 —— 斐波那契、费马小定理、矩阵快速幂

    题目链接:https://vjudge.net/problem/HDU-4549 M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Li ...