codeforces B. Petya and Exam (類比)

來源:互聯網
上載者:User
B. Petya and Exam time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard 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

aba?a2aaaaab
output
YESNO
input
abca?a?a*4abacabaabacaapapaaaaaax
output
NOYESNOYES
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".
題目連結:http://codeforces.com/contest/832/problem/B 題目大意:第一行代表good word,第二行是模式串,?可以代替任意一個good word,*可以匹配為空白,也可以代替任意一個全部都是bad word的子串,給定n個主串,問能否和模式串匹配,能就輸出“YES”,不能就輸出“NO”。 解題思路:類比。痛點在於*處的判斷,考慮*匹配為空白和為 bad word的子串兩種情況,注意除了*匹配的部分外,其他部分的字元是一一對應的,即其他部分的長度相等,可以從這一點入手。 代碼如下:

#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++){   //標記good worda[gd[i]-'a'] = 1;}cin >> s;len = s.length();for(int i=0;i<len;i++){    //判斷模式串中是否有*if(s[i]=='*'){flog = true;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 ){  //如果模式串中沒有*,串長相等才能匹配,如果有*,模式串串長要大於主串串長減1才匹配printf("NO\n");continue;}int k = 0;for(int j=0;j<ls,k<lc;j++,k++){if(s[j]<='z' && s[j] >= 'a'){   //主串的字母要和模式串的字母匹配if(s[j] != cur[k]){p = false;break;}}else if(s[j] == '?'){        //?匹配good wordint 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++){ //*後面剩下的長度要相等才匹配x = cur[k]-'a';if(a[x]){          //*匹配bad wordp = false;break;}}if(p){if(lc - k < ls - j )   //*匹配為空白的情況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");elseprintf("NO\n");}}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.