CODEFORCE 832B

来源:互联网 发布:互联网软件开发工资 编辑:程序博客网 时间:2024/06/04 23:51

Petya and Exam
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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”.

首先判断是否有’*’,如果没有必须长度一样,如果有,长度之差必须大于等于-1(最多就是多个’*’),然后串被分成了三部分,*号之前,*之后,以及*所代表的不是good串的部分,逐个匹配就好。

#include<stdio.h>#include<string.h>int isgood[26]; char gd[30], wild[101010]; int wcn, asix=-1;char str[101010]; int len;int match(){    if(asix < 0){        if(len != wcn) return 0;        for(int i=0; i<len; i++){            if(wild[i] == '?' && !isgood[str[i]-'a']) return 0;            if(wild[i] != '?' && str[i]!=wild[i]) return 0;        }        return 1;    }    if(len < wcn-1) return 0;    for(int i=0; i<asix; i++){        if(wild[i] == '?' && !isgood[str[i]-'a']) return 0;        if(wild[i] != '?' && str[i]!=wild[i]) return 0;    }    for(int i=asix+1; i<wcn; i++){        int j = len-1 - (wcn-1 - i);        if(wild[i] == '?' && !isgood[str[j]-'a']) return 0;        if(wild[i] != '?' && str[j]!=wild[i]) return 0;    }    for(int i=asix; i<len-1 - (wcn-1 - (asix+1)); i++){        if(isgood[str[i]-'a']) return 0;    }    return 1;}int main(){    scanf("%s\n%s", gd, wild); wcn = strlen(wild);    for(int i=0; gd[i]; i++) isgood[gd[i]-'a']=1;    for(int i=0; i<wcn; i++){        if(wild[i] == '*') asix = i;    }    int Q;    for(scanf("%d",&Q); Q--;){        scanf("\n%s", str); len=strlen(str);        puts(match() ? "YES" : "NO");    }    return 0;}
原创粉丝点击