B. Petya and Exam (thinking) __ thinking

Source: Internet
Author: User
Tags lowercase
B. Petya and Exam time limit/test 2 seconds memory limit per test 256 megabytes input standard input output standard o Utput

It ' s hard times now. Today Petya needs to score points on informatics exam. The tasks seem easy to Petya, but him thinks he lacks the "finish them all" and "he asks" to "help" with one.

There is a glob the statements (a string consisting of lowercase Chinese letters, characters "?" and "*"). It is known this character "*" occurs no more than once in the pattern.

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

Everything seemed easy to Petya, but then he discovered this special pattern characters differ from their usual meanin G.

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

The good letters are given to Petya. All of the others are bad. Input

The contains a string with length from 1 to consisting of distinct lowercase 中文版 letters. These letters are good letters and all of the others are bad.

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

It is guaranteed this total length of all query strings isn't 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 ' the ' I example we can replace '? ' with good letters ' a ' and ' B ', so we can- S "YES", and the answer for the second query is "NO", because we can ' t match the third letter.

Explanation of the second example. The "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" and in which both letters are good. The second query: "YES", because characters "?" can is replaced with corresponding good letters, and character "*" can Replaced with empty string, and the strings is coincide. The third query: "NO", because characters "?" can ' t be replaced with a bad letters. The fourth query: "YES", because characters "?" can is replaced with good letters "a", and character "*" can be replaced W ith a string of bad letters "x".
Making a map record is bad letter or good letter to prevent timeouts. 1 for each given string tmp[] if his length is small and the sample string length-1 (because the * can be empty) definitely no. 2 then on the tmp[] traversal, encountered * jumped from the back to look forward (* only 1) record I, after the traversal of two cases. 3 traversal may ss[] The boilerplate string is also traversed Yes ss[] left 1 * Yes otherwise no. 4 did not traverse, from the back of the execution 2 record out of the Coordinate II 5 for (I-II) see whether can replace with *.
Suggested to look at this similar problem, the solution is not the same http://blog.csdn.net/sunmoonvocano/article/details/76161439

#include <iostream> #include <cstdio> #include <set> #include <queue> #include <vector> #
include<map> #include <climits> #include <cstring> #include <algorithm> using namespace std;
Map <char,int> MP;
Char s[30],ss[100100],tmp[100010];
    int main () {int n;
    CIN >> S >> SS >> N;
    GetChar ();
    for (int i = 0;s[i];i++) mp[s[i]] = 1;
        while (n--) {bool Yes = 1;
        Gets (TMP);
        Int j = 0,i = 0;
        int II = STRLEN (tmp)-1,JJ = strlen (ss)-1;
            if (ii < jj-1) {printf ("no\n");
        Continue
                for (; tmp[i]; i++) {if (tmp[i) = = Ss[j]) {j + +;
            Continue
            if (ss[j] = = '? ')
                {if (Mp[tmp[i]]) j + +;
                    else {printf ("no\n");
                   Yes = 0; Break
            } else if (ss[j] = = ' * ') break;
                else {printf ("no\n");
                Yes = 0;
            Break
        } if (!yes) continue;
                if (!tmp[i]) {if (ss[j] = = 0) {printf ("yes\n");
            Continue
            } if (Ss[j]!= ' * ') printf ("no\n");
                else {if (ss[j+1] = = 0) printf ("yes\n");
            else printf ("no\n");
        } continue;
                for (; II >= i;ii--) {if (tmp[ii] = = Ss[jj]) {jj--;
            Continue
            if (ss[jj] = = '? ')
                {if (Mp[tmp[ii]]) {jj--;
                    else {printf ("no\n");
         Yes = 0;           Break
            } else if (ss[jj] = = ' * ') break;
                else {printf ("no\n");
                Yes = 0;
            Break
        } if (!yes) continue;
                for (int k = i; k <= II; k++) {if (mp[tmp[k)]) {printf ("no\n");
                Yes = 0;
            Break
    } if (yes) printf ("yes\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.