Petya and Exam-codeforces 832B

Source: Internet
Author: User
Tags lowercase strlen
Title:

B. Petya and Exam

It ' s hard times now. Today Petya needs to score the 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 中文版 letters, characters "?" and "*"). It is known this character "*" occurs no more than once in the pattern.

Also, n query strings is given, it's required to determine for each of the them if the pattern matches it or not.

Everything seemed easy to Petya, and then he discovered that the special pattern characters differ from their usual me Aning.

A pattern matches a string if it is possible to replace each character "?" with one good lowercase 中文版, and the Character "*" (if there is one) with any, including empty, string of bad lowercase Chinese letters, so, the resulting String is the same as the given string.

The good letters is given to Petya. All the others is bad.

Input
The first line contains a string with length from 1 to consisting of distinct lowercase 中文 letters. These letters is good letters, all the others is bad.

The second line contains the PATTERN-A string s of lowercase 中文版 letters, characters "?" and "" (1≤|s| ≤105). It is guaranteed this 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 the them contains single non-empty string consisting of lowercase Chinese letters-a query string.

It is guaranteed, the total length of all query strings are 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 I S "YES", and the answer for the second query are "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-to match The query string is to replace it with the string "Ba", in which both letters is good. The second query: "YES", because characters "?" can be replaced with corresponding good letters, and character "*" Can is Replaced with empty string, and the strings would 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 W ith a string of bad letters "x". Test Instructions:

The first line gives you a good string, then the second line is a wildcard string, then an n, followed by n-line bad string. In a wildcard string? can become a character in a good string, * can become a string in a bad string or a character, it can also become a null character, but only if the characters that are to be changed are not in the good string. Ideas:

Just follow test instructions, and you'll be fine. * The substituted string cannot appear in the good, so WA is. implementation:

Main.cpp//L////Created by Lucienshui on 2017/7/15. COPYRIGHT©2017 year Lucienshui.
All rights reserved.
#include <bits/stdc++.h> using namespace std;
#define MAXN 100005 Char 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_s
    Tdio (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; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.