codeforces 832B ——Petya and Exam

来源:互联网 发布:长沙哪里有学美工的 编辑:程序博客网 时间:2024/05/17 04:57

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
aba?a2aaaaab
output
YESNO
input
abca?a?a*4abacabaabacaapapaaaaaax
output
NOYESNOYES
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". 



先处理出长度的差别,再根据*的位置做判断。因为长度差确定了,*会代替多少个字符也。暴力匹配就行。




#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<queue>#include<algorithm>using namespace std;typedef long long int ll;string s,s1,s2;int n;int flag;int main(){    cin>>s>>s1>>n;    bool flaggy;    int len1=s1.length();    int len2;    if(s1.find('*')!=-1)        flaggy=true;    else        flaggy=false;    for(int i=0;i<n;i++){        cin>>s2;        len2=s2.length();        int ok=1;        if(flaggy){            if(len2<len1-1){                printf("NO\n");                continue;            }            int bound=len2-len1;            for(int i=0,j=0;j<len2&&ok;i++,j++){                if(s2[j]==s1[i])                    continue;                if(s1[i]=='?'&&s.find(s2[j])!=-1)                    continue;                else if(s1[i]=='?')                    ok=0;                if(s1[i]=='*'){                    if(bound==-1)                        j--;                    else{                        for(int k=0;k<=bound&&ok;k++,j++){                            if(s.find(s2[j])==-1)                                continue;                            else                                ok=0;                        }                        j--;                    }                }                else                    ok=0;            }            if(ok)                printf("YES\n");            else                printf("NO\n");        }        else{            if(len2!=len1){                printf("NO\n");                continue;            }            for(int i=0;i<len1&&ok;i++){                if(s1[i]==s2[i]||(s1[i]=='?'&&s.find(s2[i])!=-1))                    continue;                else                    ok=0;            }            if(ok)                printf("YES\n");            else                printf("NO\n");        }    }    return 0;}


原创粉丝点击