B. Petya and Exam【字符串】

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

题意:第一行是好字符(26个),除此之外都是坏字符,第二行是B字符串,B中问号可以change为好字符,* 只能change为坏字符串(字符可以有可以0~MAX个),接下来n次询问,每次给出目标C字符串,问B可以change为C么?

思路:,这场怎么感觉有点难(自己太水),一开始没想写,感觉复杂度过不了,没想到没被hack(运气好吧);

先预处理一下好字符和判断一下B中是否有 * 存在,其次比较B和C的长度(和*是否存在有关,具体代码中有),根据B,C的长度加一些特判,特判之后其余的就是模拟了,关键是 * 部分的处理,注意代码中 cnt 的作用;

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define max_n 100010
char a[max_n],b[max_n],c[max_n];
int d[1010];

int main()
{
	int n,res=0;
	memset(d,0,sizeof(d));
	scanf("%s",a);
	getchar();
	for(int i=0;i<strlen(a);i++)
	{
		d[a[i]]=1;
	}
	scanf("%s",b);
	getchar();
	for(int i=0;i<strlen(b);i++)
	{
		if(b[i]=='*')
		{
			res++;
			break;
		} 
	}
	int len1=strlen(b);
	scanf("%d",&n);
	while(n--)
	{
		bool flag=true;
		memset(c,0,sizeof(c));
		scanf("%s",c);
		int cnt=0;
		int len2=strlen(c);
		int len=max(len1,len2);
		int ans=abs(len1-len2);
		if(res==0 && ans!=0) //防hack,加了几个特判 
		{
			printf("NO\n");
			continue;
		}
		if(res==1 && len1-len2>=2)
		{
			printf("NO\n");
			continue;
		}
		for(int i=0;i<len1;i++)
		{
			if(b[i]=='?')
			{
				if(d[c[i+cnt]]!=1) 
					flag=false;
			}
			else if(b[i]=='*')
			{
				 for(int j=i;j<=len2-len1+i;j++)
				 {
				 	if(d[c[j]])
					 {
					 	flag=false;
					 	break;
					 } 
				 }
				 cnt=len2-len1;
			}
			else
			{
				if(b[i]!=c[i+cnt])
					flag=false;
			} 
		//	printf("%c %c %d\n",b[i],c[i+cnt],cnt);
			if(!flag) break;
		}
		if(flag) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
} 


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值