Petya and Exam

http://codeforces.com/problemset/problem/832/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

ab
a?a
2
aaa
aab

Output

YES
NO

Input

abc
a?a?a*
4
abacaba
abaca
apapa
aaaaax

Output

NO
YES
NO
YES

题意:题目大意是先给你一个好串,之后在好串的基础上加上 ?或者 *字符成为一个新串(即该题的源字符串),其中?可以换成任意一个好串,而 * 则可以换成任意一个不好的字符串 ,也可以为空。同时 * 只能出现一次 ,所以如果出现 * ,衍生字符串只能比源字符串的长度多出他们的差值,即设源字符串长度是 len2 ,衍生字符串长度为 len3 ,* 衍生出的字符串长度为 len=len3l-len2+1.

   后买给出几个衍生字符串,判断能否从源字符串变化而来,能则输出YES,否 输出NO。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAX 100002
using namespace std;
char s1[MAX],s2[MAX],s3[MAX];
int dis[MAX];
int n;
int main()
{
    scanf("%s",s1);
    int len1=strlen(s1);
    for(int i=0;i<len1;i++)
        dis[s1[i]]=1;
    scanf("%s",s2);
    int len2=strlen(s2);
    int ff=0;//用来标记‘*’是否出现
    for(int i=0;i<len2;i++)
     if(s2[i]=='*')
        ff=1;
    cin>>n;
    while(n--)
    {
        int flag=0;
        scanf("%s",s3);
        int len3=strlen(s3);
        int num2=0,num3=0;
        if(len3<len2-1&&ff)//对字符串长度进行判定
            printf("NO\n");
        else if(len3<len2&&!ff)
                printf("NO\n");
            else if(len3>len2&&!ff)
                printf("NO\n");
        else
        {
            int len=len3-len2+1;
            for(int i=0;i<len2;i++)
            {
                if(s2[i]=='?')
                {
                    if(!dis[s3[num3]])
                    {
                        flag=1;
                        break;
                    }
                    num3++;
                }
                else if(s2[i]=='*')
                {
                    for(int j=i;j<i+len;j++)//判断*换成的字符串是否全是坏串
                    {
                        if(dis[s3[j]])
                        {
                            flag=1;
                            break;
                        }
                    }
                    num3+=len;
                }
                else
                {
                    if(s2[i]!=s3[num3])
                    {
                        flag=1;
                        break;
                    }
                    num3++;
                }
                if(flag)
                    break;
            }
            if(flag)
                printf("NO\n");
            else
                printf("YES\n");
        }
    }
    return 0;
}

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值