Codeforces B. Petya and Exam (analog)

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

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 meanin G.

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".
Topic Link: Http://codeforces.com/contest/832/problem/B title: The first line represents good word, the second line is the pattern string,? can replace any one good word,* can be matched to null, can also replace any one is all bad word's substring, given n the main string, ask whether and pattern string matching, can output "YES", cannot output "no". Problem-solving ideas: simulation. The difficulty lies in the judgment of the *, considering that * match is empty and for bad word's substring two cases, note in addition to the * matching part, the other part of the character is one by one corresponding, that is the other part of the length is equal, you can start from this point. The code is as follows:

#include <cstdio> #include <cstring> #include <string> #include <iostream> using namespace std;
int a[26];
	int main (void) {string gd,s;
	Cin >> GD;
	memset (A,0,sizeof (a));
	int Len;
	Len = Gd.length ();
	BOOL flog = false;
	for (int i=0;i<len;i++) {//Tag good word a[gd[i]-' a '] = 1;
	} CIN >> S;
	Len = S.length ();
			for (int i=0;i<len;i++) {//To determine if there is * if (s[i]== ' * ') {flog = true) in the pattern string;
		Break
	}} int n;
	scanf ("%d", &n);
		for (int i=0;i<n;i++) {string cur;
		CIN >> cur;
		BOOL p = true;
		int ls = s.length ();
		int lc = Cur.length ();
			if (ls > lc+1 | | ls = = lc+1 &&!flog) {//if there is no * in the pattern string, the string length is equal to match, if there is *, the pattern string string length is greater than the main string length minus 1 to match printf ("no\n");
		Continue
		} int k = 0; for (int j=0;j<ls,k<lc;j++,k++) {if (s[j]<= ' z ' && s[j] >= ' a ') {//the letter of the main string to match the letter of the pattern string if (s[j]! = CU
					R[k]) {p = false;
				Break }} else if (s[j] = = '? ')
				{//? match good word int x = cur[k]-' a '; if (!A[x]) {p = false;
				Break
				}} else if (s[j] = = ' * ') {int x = cur[k]-' a ';
					For (K < lc-(ls-j) +1; k++) {//* The remaining length is equal to match x = cur[k]-' a ';
						if (A[x]) {//* matches bad word p = false;
					Break
					}} if (p) {if (Lc-k < ls-j)//* match for empty case k--;
				Continue
				}} if (j = = Ls-1 && k < lc-1) {p = false;
			Break
				} if (k = = Lc-1 && (J < ls-2 | | j = = ls-2 && s[ls-1]!= ' * ')) {p = false;
			Break
		}} if (p) printf ("yes\n");
	else printf ("no\n"); }
}


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.