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. 文件另存为——Autocad.doc.SaveAs

    一.前言 使用pyautocad编辑好cad图纸后,往往涉及到一个保存的问题,但是官方文档并未提及,所以只能自己来了,测试了好久,终于是找到了保存的命令和参数说明. 二.方法介绍 Autocad.do ...

  2. memcache及其telnet命令使用详解

    1.启动Memcache 常用参数memcached 1.4.3-p <num>      设置端口号(默认不设置为: 11211)-U <num>      UDP监听端口 ...

  3. LeetCode(15)题解--3Sum

    https://leetcode.com/problems/3sum/ 题目: Given an array S of n integers, are there elements a, b, c i ...

  4. lnmp下 nginx 配置虚拟主机

    <一.参考> 这里以配置2个站点(2个域名)为例,n 个站点可以相应增加调整,假设: IP地址: 202.55.1.100 域名1 example1.com 放在 /www/example ...

  5. Theseven relationsarein threecategories:equivalent, congruent, andsimilar.

    http://www.math.pitt.edu/~xfc/math2370/chap5.pdf

  6. 笔记,将CString转换为数值

           ANSI UNICODE TCHAR int    atoi    _wtoi  _ttoi long       atol    _wtol _ttol double   atof   ...

  7. 无法启动FTP站点,服务目前停止

    在本地搭建了一个FTP服务器(windows搭建FTP服务器实战),再启动的时候提示错误: 错误提示信息: 根据提示可以查出问题原因:FTP服务没有开启 启动服务,再次重启站点服务.一切OK. 亲测好 ...

  8. Windows下配置PHPUnit(pear已弃用,使用phpunit.phar)

    一.配置PHPUnit 首先到PHPUnit官网(点此进入)下载相应的版本.php 5.5及以下版本请使用PHPUnit 4.8.得到 .phar 文件,并把名字改为 phpunit.phar . 把 ...

  9. FFMPEG more samples than frame size (avcodec_encode_audio2) 的解决方案

    在实际的项目中,从音频设备采集到的音频的类型和编码器类型(aac ,amr)通常是不一致的. 那么我们首先需要做重采样的过程.利用swr_convert 重新采样. 这时候我们可能会遇到另外一个问题. ...

  10. hdu 1361.Parencodings 解题报告

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1361 题目意思: 根据输入的P-sequence , 输出对应的W-sequence.   P-se ...