Codeforce_832B_Petya and Exam_字符串模拟

32 篇文章 0 订阅

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

题意: 先给你一个字符串s,里面的都是合法字符;
在给你 s1串 中间夹杂* 型号可为空或者用非法字符或者非法字符组成的字符串取代;
接下来是n个询问;

想了一下午才想出来,自己有大致思路,但是没有考虑到非法情况就是一直wa

思路:

先判断取代”*”号的字符串是否合法,预处理出所有?位置判断是否合法,最后判断其余字符是否合法,只有’星号’是两个字符串长度才会不一致要特判.

#include<bits/stdc++.h>
using namespace std;
#include <ext/hash_map>
using namespace __gnu_cxx;
string s,s1,s2,st;
int num[111111];
int main()
{
    int n,i,j;
    while(cin>>s>>s1)
    {
        hash_map<char,int> stu;
        stu.clear();
        for(i=0; i<s.length(); i++)
            stu[s[i]]=1;
        int k=0,post=-1;
        for(i=0; i<s1.length(); i++)
        {
            if(s1[i]=='*')
                post=i;
            if(s1[i]=='?')
                num[k++]=i;
        }
        cin>>n;
        //    cout<<"post "<<post<<endl;
        while(n--)
        {
            int f=1, f1=1;
            cin>>s2;
            int s2_len=s2.length();
            int s1_len=s1.length();
            int st_len=s2_len-s1_len+1;
            //     cout<<"s1 "<<s1_len<<" s2 "<<s2_len<<" st "<<st_len<<endl;
            if(st_len<0)
            {
                puts("NO");
                continue;
            }
            if(post==-1&&k==0)
            {

                    if(s1==s2) puts("YES");
                    else puts("NO");
                continue;
            }
            st.clear();
            if(post>=0)
            {

                for(i=post; i<post+st_len; i++)
                {
                    if(stu[s2[i]])
                    {
                        f=0;
                        break;
                    }
                }
                //   cout<<"st= "<<st<<endl;
            }
            if(!f)
                puts("NO");
            else
            {
                for(i=0; i<k; i++)
                {
                    if(!f1) break;
                    if(num[i]<post)
                    {
                        if(!stu[s2[num[i]]])
                        {
                            f1=0;
                            break;
                        }

                    }
                    else
                    {
                        if(!stu[s2[num[i]+st_len-1]])
                        {
                            f1=0;
                            break;
                        }
                    }
                }
                //   cout<<f<<" "<<f1<<endl;
                if(!f1)
                    puts("NO");
                else
                {

                    if(post==-1&&s2_len>s1_len)
                    {
                        puts("NO");
                        continue;
                    }
                    int k=0;
                    for(i=0; i<s1_len; i++,k++)
                    {
                        if(i==post)
                        {
                            k+=st_len-1;
                            continue;
                        }
                        if(s1[i]=='?')
                            continue;
                        if(s1[i]!=s2[k])
                        {
                            f1=0;
                            break;
                        }
                    }
                    if(f1)
                        puts("YES");
                    else puts("NO");
                }
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值