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

题意:
1.第一个给出的字符串a是由不同的小写英文字母组成。这些字母都是好字母,其它都是不好字母;
2.给的是一个语句(或者说字符串b),并且b中有一个glob模式(一个由小写英文字母组成的字符串,字符“?”“*”)。众所周知,字符“*”在glob模式中不会出现一次。
3.给出了一个数n代表有n个查询字符串;
n个查询字符串,需要为每个查询字符串确定模式是否匹配。

字符“?”可以用一个好字母替换;字符“*”(如果有的话)可以用任何(包括空的)坏的小写英文字母串;

如果能使替换成功使得他们一样成为glob模式则匹配成功输出YES,否则输出NO;
翻译:
输入
第一行包含一个长度为1到26的字符串,由不同的小写英文字母组成。这些字母是好字母,其他字母都不好。
第二行包含模式 - 一个由小写英文字母组成的字符串,字符“?”和“”(1≤| s |≤105)。保证字符“”在s中出现不超过一次。
第三行包含整数n(1≤n≤105) - 查询字符串的数量。
接下来是n行,每行包含由小写英文字母组成的单个非空字符串 - 查询字符串。

解题思路:模拟;

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
const int N=100500;
char a[500],b[N],s[N];
int vis[500];
int cnt;
int main()
{
    cin>>a>>b;
    int n;
    int k=strlen(a),m=strlen(b),cnt=0;
    for(int i=0; i<k; i++)//开一个数组标记好字母;
    {
        vis[a[i]]=1;
    }
    for(int i=0; i<m; i++)//记下出现*的次数;
    {
        if(b[i]=='*')
            cnt++;
    }
    scanf("%d",&n);
    while(n--)
    {
        int flag=1;
        cin>>s;
        int len=strlen(s);
        if(cnt==0&&len!=m)//如果*在s中没有出现过,则判断s与b字符串长度是否相等;
            printf("NO\n");
        else if(cnt==1&&m-len>1)//如果出现一次,b字符串的长度比s超过1则必不能匹配;
            cout<<"NO"<<endl;
        else
        {
            int is=0,ib=0,l=len-m+1;//可能存在s长度比b多很多,它有可能是一个包括任何(包括空的)坏的小写英文字母串组成;
            while(is<len)//查找
            {
                if(flag==0)//结束
                    break;
                if(b[ib]!='?'&&b[ib]!='*')//当b不是?或者*时,只要s与b的对应位置的字母不相同,必不匹配;
                {
                    if(s[is]!=b[ib])
                    {
                        flag=0;
                        cout<<"NO"<<endl;
                        break;
                    }
                }
                else if(b[ib]=='?')//当b出现?时,判断vis标记的为零则必不匹配;
                {
                    if(vis[s[is]]==0)
                    {
                        flag=0;
                        cout<<"No"<<endl;
                        break;
                    }
                }
                else if(b[ib]=='*')//为*
                {
                    for(int j=0; j<l; j++)
                    {
                        if(vis[s[is+j]]==1)
                        {
                            flag=0;
                            cout<<"NO"<<endl;
                            break;
                        }
                    }
                    is=is+l-1;//注意这时,is需要移位,其实本质是为了与ib对应:相当于删了那些坏的任何(包括空的)坏的小写英文字母串;然后继续判断;
                }
                is++,ib++;
            }
            if(flag)
                printf("YES\n");
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
解法:贪心算法 如果两个字符串不相等,那么它们必定至少有一位不一样。考虑对于这一位,我们应该对字符串 a 进行哪种操作,才能使得它更接近于字符串 b。 首先,我们可以通过交换字符串 a 中的两个数字,使得这一位变为我们想要的数字。如果我们把这一位变成了 b 中的数字,那么显然这一位就不需要再进行修改了。因此,我们只需要考虑把这一位变成 a 中的数字 4 或 7。 如果我们把这一位变成 a 中的数字,则需要执行一次操作;如果我们把这一位变成 a 中的数字,则需要执行一次操作。那么,我们应该采取哪种操作呢? 我们可以贪心地想,如果我们把这一位变成 a 中的数字,那么这一位和 b 中的数字就越相似,那么接下来的操作就越容易执行。因此,我们应该选择将这一位变成 a 中的数字,从而尽可能地增加和 b 相同的数字的数量。 实现时,我们可以从左到右扫描字符串 a 和 b,统计它们不同的位置的数量。对于每个不同的位置,我们都可以选择将这一位变成 4 或 7,然后更新 a 中数字 4 和 7 的数量。最终,我们就可以得到将字符串 a 转换为字符串 b 所需的最少操作数。 时间复杂度 字符串 a 和 b 的长度为 n,我们需要扫描一遍字符串并统计数字 4 和 7 的数量,因此时间复杂度为 O(n)。 空间复杂度 我们只需要存储数字 4 和 7 的数量,因此空间复杂度为 O(1)。 Python 代码

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值