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

相当难的一道模拟,题目大意是说先给你两个字符串,第一个字符串中的字符可以看作是好字符,第二个字符串中有?和 * ,?表示可以替换为任何一个好字符,而 * 表示可以替换为任何一个不含好字符的字符串。再给一系列的字符串,看是否可以由第二个字符串替换为当前字符串。

其实没什么考点,就是模拟,但是模拟过程比较复杂。

第一种情况是没有*的时候,这种情况下可以先比较字符串B和C的长度,因为这种情况下两个字符串长度必然是相等的,长度相等情况下,比较每个字符,字符不相同的时候直接不符合条件,如果B对应位置是?,就需要看是不是可以在A里找到,如果找得到说明可以替换,按照这个思路继续找即可。

第二种情况是有?的时候,因为?可以替换为任意长度的字符串,所以不能用比较长度的方法,需要先记录* 的位置,将字符串B分成* 前后两部分,先比较前面,依然按照第一种情况的方式比较。后半部分的比较就要稍微区别一下,这里采用倒着检验的方法,因为* 替换的字符串长度不确定,倒着比较到第一个没法替换的字符,然后假定这个字符和*之间的都是替换来的,看看这个字符串是不是符合不含任何一个A中的字符,如果符合就说明这个字符是替换来的,此外如果C的长度直接小于了 *的位置,直接就不可能替换。

结合上面的这两种情况,写代码即可。

AC代码

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{string A,B;cin>>A>>B;int k=-1;for(int i=0;i<B.length();i++)if(B[i]=='*'){k=i;break;}int n;cin>>n;while(n--){string C;cin>>C;if(k==-1){if(B.length()!=C.length()) cout<<"NO"<<endl;else{int flag=1;for(int i=0;i<C.length();i++){if(B[i]=='?'){if(A.find(C[i])==-1)flag=0;}  else if(B[i]!=C[i])flag=0;if(flag==0) break;}if(flag==1) cout<<"YES"<<endl;else cout<<"NO"<<endl;}}else{int flag=1;for(int i=0;i<k&&flag==1;i++){if(B[i]=='?'){if(A.find(C[i])==-1)flag=0;}else if(B[i]!=C[i])flag=0;}int i=C.length()-1,j=B.length()-1;for(;j>k&&flag==1;i--,j--){if(i<k){flag=0;break;}if(B[j]=='?'){if(A.find(C[i])==-1){flag=0;break;}        }else if(B[j]!=C[i]){flag=0;break;}       }   for(int t=k;t<=i&&flag==1;t++)if(A.find(C[t])!=-1)flag=0;if(flag==0) cout<<"NO"<<endl;else cout<<"YES"<<endl;}}return 0;
}

Petya and Exam 模拟相关推荐

  1. Petya and Exam(字符串模拟)

    Petya and Exam - CodeForces 832B - Virtual Judge (csgrandeur.cn) 题意:给你一个好的字符串和一个模式串,模式串里面的?可以替换成任意的好 ...

  2. C. Petya and Exam

    C. Petya and Exam 题目链接 题目大意,考试t个小时,n个题,简单题要用a个小时,难题要花b个小时,每题1分,可随时交卷,但考试过程中会随着时间的延长,题目会变成必做题,如果交卷时必做 ...

  3. B. Petya and Exam

    题目链接 It's hard times now. Today Petya needs to score 100 points on Informatics exam. The tasks seem ...

  4. Petya and Exam

    Description It's hard times now. Today Petya needs to score 100 points on Informatics exam. The task ...

  5. 泛型相关用法(day3)

    一.instanceof 用法总结. instanceof属于java关键字之一,instanceof 严格来说是Java中的一个双目运算符,用来测试一个对象是否为一个类的实例,用法为:boolean ...

  6. 我爱记单词系统-C++实现

    一.课程设计目的 实践能力是工程技术人才必须具备的能力.本课程设计是为<面向对象程序设计>课程设置的实践教学环节,目的是培养学生综合运用面向对象程序设计的基本理论.方法和相关专业知识分析. ...

  7. C++课程设计——背单词程序

    C++课程设计--背单词程序 设计要求 设计一背单词程序,程序以菜单方式工作,字典使用文本文件存放就可以了.使之能提供以下功能:添加新词 支持顺序测验和随机测验 记录没背过的单词 查找单词的汉语或英语 ...

  8. python 模拟考试系统_Project-OTS: Online Exam System written on Python 3. 基于Python 3的在线考试系统。...

    Project-OTS Online Exam System written on Python 3. Your can first try it on ots.icystal.top. 基于Pyth ...

  9. 基础算法 —— 模拟思维

    [概述] 模拟,是根据实际问题建立模型,模拟实际按程序走一遍,最终求出答案. 思维,则是与逻辑思维有关,其需要针对题意.数据范围等抽丝剥茧抽离出有用的信息,从而得出一个结果. 对于一般的模拟,直接根据 ...

最新文章

  1. ubuntu 16.04 python3 使用ryu
  2. hdu 2842 Chinese Rings 矩阵快速幂
  3. iOS UILable高度自适应
  4. SpringBoot高级-缓存-RedisTemplate序列化机制
  5. Linux基础_Hadoop环境搭建必备
  6. 定时器精度对性能的影响_Comet CAA-500天线分析仪 | 高精度模拟十字针同时显示SWR和阻抗...
  7. android之阴影效果
  8. python图像坐标系_世界坐标系、相机坐标系和图像坐标系的转换(Python)
  9. wp网站,wordpress网站搭建,wp网站建设教程
  10. 一文了解 Serverless 究竟是什么
  11. SQL性能优化以及性能测试
  12. matlab 椭圆方程拟合,matlab中如何插值拟合求椭圆方程
  13. 利用Excel制作问卷的饼状图
  14. ubuntu14.04编译安装strongswan
  15. subset selection
  16. XXL-JOB适配人大金仓数据库kingbase
  17. 语音识别——解码器(WFST、Lattice)
  18. 【unity shader】高级光照 --- 薄膜干涉
  19. 数据结构与算法(一)
  20. 系统重装后MySQL的数据恢复

热门文章

  1. QStackedWidget设置无效问题
  2. 20150217 IMX257实现GPIO-IRQ中断按键驱动程序
  3. php图片遍历,php – 如何遍历图像的所有像素?
  4. 更改数据库表中有数据的字段类型NUMERIC(18,2)为NUMERIC(18,6)
  5. 线性基的一些基础模版
  6. 上海新中考体育考试方案公布:总分30分不变
  7. Cocoapods应用(001-简介以及安装和卸载)
  8. 20165115 2017-2018-2 《Java程序设计》第六周学习总结
  9. laravel 环境自编译过程
  10. 为什么要用maven - 1