Codeforces Round #425 (Div. 2) 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".
题意:第一串是好串。里面的字符全是好字符。其他的包括空格都是坏字符。

第二串是模版串 ?里必须填好字符。*可以是空格或者坏串(注:坏串,可以是多个字符,你懂我意思吧。)

然后下面的就问你匹不匹配


idea:(这什么(LJ)题。放B卡我一晚上。这场枪战体验很差,我要投诉~)正向匹配反向匹配就可以忽略掉*里字符多少的问题。WA 12的话一定是*能跳字符的多少不对。

//china no.1
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;

#define pi acos(-1)
#define endl '\n'
#define srand() srand(time(0));
#define me(x) memset(x,0,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0); cin.tie(0);
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,1,-1,-1,1};
const int dy[]={0,1,0,-1,-1,1,-1,1};
const int maxn=1e3+10;
const int maxx=1e5+100;
const double EPS=1e-7;
const int MOD=10000007;
#define mod(x) ((x)%MOD);
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define W while
#define sgn(x) ((x) < 0 ? -1 : (x) > 0)
#define bug printf("***********\n");


inline int Scan()
{
    int Res=0,ch,Flag=0;
    if((ch=getchar())=='-')Flag=1;
    else if(ch>='0' && ch<='9')Res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')Res=Res*10+ch-'0';
    return Flag ? -Res : Res;
}

int t;
string s1,s2;
int a[1010];
bool pfx[maxx],sfx[maxx];
int main()
{
    cin>>s1>>s2;
    for(int i=0;i<s1.size();i++)
        a[s1[i]]++;
    int val=-1;
    for(int i=0;i<s2.size();i++)
        if(s2[i]=='*')
    {
        val=i;
        break;
    }
    int tl=s2.size();
    cin>>t;
    while(t--)
    {
        string s3;
        cin>>s3;
        int sl=s3.size();
        int flag=0;
        for(int j = 0; j < sl && j < tl; ++j)//正向匹配
            pfx[j] = (j == 0 ? true : pfx[j - 1])&&
            (s2[j] == s3[j] || (s2[j] == '?' && a[s3[j]]));
        for(int j = 0; j < sl && j < tl; ++j)//反向匹配
            sfx[j] = (j == 0 ? true : sfx[j - 1])&&
            (s2[tl - 1 - j] == s3[sl - 1 - j] ||
             (s2[tl - 1 - j] == '?' && a[s3[sl - 1 - j]]));
        if(val==-1)//没有* 并且等串
            if(tl == sl && pfx[tl - 1])
            {
                puts("YES");
                continue;
            }
            else
            {
                puts("NO");
                continue;
            }
        else //有* 可能不等串
        {
            for(int i=val;i<=sl-tl+val;i++)
            {
                if(!a[s3[i]])//中间差必须是坏串
                    continue;
                else flag=1;
            }
            if(tl>sl+1)
                flag=1;
            if(!(val==0?1:pfx[val-1]))
                flag=1;//有空格  第一个位置有*吗 不然的话看前一个位置是否匹配
            if(!(val==tl-1?1:sfx[tl-2-val]))
                flag=1;//是否是最后个位置有* 不然的话只需看*后一位是否匹配
        }
        if(flag)
            puts("NO");
        else puts("YES");
    }
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值