Petya and Exam

来源:互联网 发布:java hsv色彩转rgb 编辑:程序博客网 时间:2024/06/05 05:12

题目:

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


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

题意:

  第一行给你一个good字符串,然后第二行是通配字符串,然后一个n,跟着n行bad字符串。通配字符串中的?可以变成一个good字符串之中的字符,*可以变成坏字符串中的一串或者是一个字符,也可以变成空字符,但前提是*将要变成的字符全都没有在good字符串中出现过。

思路:

  直接跟着题意走就可以,昨晚一直没发现*所取代的字符串不能在good里出现过,所以WA了。

实现:

////  main.cpp//  L////  Created by LucienShui on 2017/7/15.//  Copyright © 2017年 LucienShui. All rights reserved.//#include <bits/stdc++.h>using namespace std;#define maxn 100005char s[maxn],t[maxn];bool good[maxn];int main (int argc, char* argv[]) {#ifndef ONLINE_JUDGE    freopen("in.txt","r",stdin);#endif    ios_base::sync_with_stdio(false);    cin.tie(nullptr);    cin >> s;    for(int i=0 ; s[i] ; i++) good[s[i]]=true;    cin >> s;    int n = int(strlen(s)),q,loc=-1;    for(int i=0 ; i<n ; i++) if(s[i] == '*') loc = i;    cin >> q;    while(q--!=0) {        cin >> t;        int m = int(strlen(t));        if(loc<0) {            if(n!=m) {                cout << "NO\n";                continue;            }            bool ok=true;            for(int i=0 ; i<n ; i++) ok &= (s[i] == t[i] || (s[i] == '?' && good[t[i]]));            cout << (ok ? "YES\n" : "NO\n");        }        else {            if(n-1>m) {                cout << "NO\n";                continue;            }            bool ok = true;            int pre=loc, suf = n-1-loc;            for(int i=0 ; i<pre ; i++) ok &= (s[i] == t[i] || (s[i] == '?' && good[t[i]]));            for(int i=0 ; i<suf ; i++) ok &= (s[n-1-i] == t[m-1-i] || (s[n-1-i] == '?' && good[t[m-1-i]]));            for(int i=pre ; i<=m-1-suf ; i++) ok &= (!good[t[i]]);            cout << (ok ? "YES\n" : "NO\n");        }    }    return 0;}