Codeforces Round #425 (Div. 2) B. Petya and Exam(字符串模拟 水)
题目链接:http://codeforces.com/contest/832/problem/B
B. Petya and Exam
2 seconds
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.
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.
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.
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".
题目大意:
给定一个字符串,都是'a'~'z'中的字符,我们定义为好字符。
给定一个母串,并询问n个子串能否有正确结果符合如下:
1.母串'?'可以替换成任何好字符
2.'*'只能被一个或多个坏字符,或空字符替换。
解题思路
简单字符串模拟,主要难在母串有'*'时候,分别比较'*'前后两部分是否符合1操作,
再询问子串其他部分是否有坏字符即可。具体看代码吧- -
AC代码
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- #define For(I,A,B) for(int I=(A);I<(B);++I)
- #define Rep(I,N) For(I,0,N)
- #define mem(A,val) memset(A,val,sizeof(A))
- const int maxn=1e5+;
- int n,len1,len2,pos;
- bool star=false,flag,vis[];
- int main()
- {
- string a,b,str;
- cin>>str>>a>>n;
- mem(vis,false);
- Rep(i,str.size())
- vis[str[i]-'a']=true; //能替换的字符我们标记为好字符
- Rep(i,a.size())
- if(a[i]=='*') { star=true;pos=i;break; }
- while(n--)
- {
- cin>>b;
- flag=false;
- len1=a.size(); len2=b.size();
- if((star&&len1-len2>) || (!star&&len2!=len1))
- { puts("NO");continue; } //长度都不符合就直接NO了
- if(!star)
- { //母串无'*'时候随便比较下就是了
- for(int i=;i<len1&&!flag;i++)
- {
- if(a[i]==b[i] || (a[i]=='?'&&vis[b[i]-'a']))
- continue;
- else flag=true;
- }
- }
- else
- { //母串有‘*’时
- for(int i=;i<pos&&!flag;i++)
- { //‘*’前面的部分,母串子串按规则比较
- if(a[i]==b[i] || (a[i]=='?'&&vis[b[i]-'a']))
- continue;
- else flag=true;
- }
- int k=len2-;
- for(int j=len1-;j>pos;j--,k--)
- { // ‘*’后面的部分,母串子串依旧要按规则比较
- if(b[k]==a[j]||(a[j]=='?'&&vis[b[k]-'a']))
- continue;
- else flag=true;
- }
- if(!flag)
- { //按题意,子串剩余部分,应该是一些坏字符
- for(int i=pos;i<=k&&!flag;i++)
- {
- if(vis[b[i]-'a']) flag=true;
- }
- }
- }
- if(flag) puts("NO");
- else puts("YES");
- }
- return ;
- }
Codeforces Round #425 (Div. 2) B. Petya and Exam(字符串模拟 水)的更多相关文章
- Codeforces Round #425 (Div. 2) B - Petya and Exam
地址:http://codeforces.com/contest/832/problem/B 题目: B. Petya and Exam time limit per test 2 seconds m ...
- Codeforces Round #481 (Div. 3) G. Petya's Exams (贪心,模拟)
题意:你有\(n\)天的时间,这段时间中你有\(m\)长考试,\(s\)表示宣布考试的日期,\(d\)表示考试的时间,\(c\)表示需要准备时间,如果你不能准备好所有考试,输出\(-1\),否则输出你 ...
- Codeforces Round #425 (Div. 2))——A题&&B题&&D题
A. Sasha and Sticks 题目链接:http://codeforces.com/contest/832/problem/A 题目意思:n个棍,双方每次取k个,取得多次数的人获胜,Sash ...
- Codeforces Round #425 (Div. 2) Problem B Petya and Exam (Codeforces 832B) - 暴力
It's hard times now. Today Petya needs to score 100 points on Informatics exam. The tasks seem easy ...
- 【Codeforces Round #425 (Div. 2) B】Petya and Exam
[Link]:http://codeforces.com/contest/832/problem/B [Description] *能代替一个字符串(由坏字母组成); ?能代替单个字符(由好字母组成) ...
- Codeforces Round #510 (Div. 2) D. Petya and Array(树状数组)
D. Petya and Array 题目链接:https://codeforces.com/contest/1042/problem/D 题意: 给出n个数,问一共有多少个区间,满足区间和小于t. ...
- Codeforces Round #425 (Div. 2)C
题目连接:http://codeforces.com/contest/832/problem/C C. Strange Radiation time limit per test 3 seconds ...
- Codeforces Round #425 (Div. 2)
A 题意:给你n根棍子,两个人每次拿m根你,你先拿,如果该谁拿的时候棍子数<m,这人就输,对手就赢,问你第一个拿的人能赢吗 代码: #include<stdio.h>#define ...
- Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状数组
Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations ...
随机推荐
- [C#.net]WinForm载入窗体完成后自动执行事件
一.以下是网络上可搜索到的次序 当 Windows Form 应用程序启动时,会以下列顺序引发主要表单的启动事件: System.Windows.Forms.Control.Handle ...
- kbmmw 中JSON 中使用SQL 查询
前面讲到了kbmmw 的JSON 对象操作,如何快速的查找JSON 中的值? 一种办法就是通过遍历的方法,其实在kbmmw 还有一种灵活的查询方式, 就是通过SQL 方式查询JSON 中的值.也就是说 ...
- 2018.11.17 hdu5829Rikka with Subset(ntt)
传送门 nttnttntt基础题. 考虑计算每一个数在排名为kkk时被统计了多少次来更新答案. 这样的话,设anskans_kansk表示所有数的值乘上排名为kkk的子集数的总和. 则ansk=∑i ...
- 防止网站被镜像,被iframe
1.防止网站被镜像 现象:他人域名访问到的是我的网站解析的ip地址我的 解决方法: http.ini里可以这样写 "RewriteCond Host: !^www.web\.cn$ Rewr ...
- python,函数
numpy.tile():参考https://www.jianshu.com/p/4b74a367833c numpy.argsort:argsort()里面传入参数只有数组时,返回的是数组值从小到大 ...
- oracle 操作,偶尔记一下
一.根据更新时间排序 1.插入insert into t_user t (t.user_id,t.user_name,t.create_time,t.update_time)values('10',' ...
- 快速排序 JavaScript 实现
作为算法目录下的第一篇博文,快速排序那是再合适不过了.作为最基本最经典的算法之一,我觉得每个程序员都应该熟悉并且掌握它,而不是只会调用库函数,知其然而不知其所以然. 排序算法有10种左右(或许更多), ...
- 严格别名规则“-fstrict-aliasing”和“-fno-strict-aliasing”及类型双关
“-fstrict-aliasing”表示启用严格别名规则,“-fno-strict-aliasing”表示禁用严格别名规则,当gcc的编译优化参数为“-O2”.“-O3”和“-Os”时,默认会打开“ ...
- 强制DataNode向NameNode上报blocks
正常情况下,什么时候上报blocks,是由NameNode通过回复心跳响应的方式触发的. 一次机房搬迁中,原机房hadoop版本为2.7.2,新机房版本为2.8.0,采用先扩容再缩容的方式搬迁.由于新 ...
- _杂谈_C语言历史
早期的操作系统软件主要是用汇编语言(包括UNIX操作系统在内)编写的.由于汇编语言依赖于计算机硬件,所以程序的可读性和可移植性都比较差,所以呢,为了提高操作系统软件的可读性和可移植性,最好改用高级语言 ...