B. Petya and Exam
来源:互联网 发布:怎么给淘宝卖家发图片 编辑:程序博客网 时间:2024/05/12 05:09
B. Petya and Exam
题目链接
题意
给你一串字符,在这个串中所有出现的字符都是\(good\)字符,未出现的都是\(bad\)字符,
然后给你另一串字符,这个字符串中有两个特殊的字符,一个是\(?\)这个字符只能被\(good\)字符代替,另一个是\(*\)这个能被忽略,或者被\(bad\)字符组成的字符串代替.
然后给你n个字符串,问你是否能通过代替原本串中的符号来变得。
思路
首先判断原本串中是否存在\(*\)字符如果不存在,那么下面的串的长度就应该和原本串的长度相等。如果存在的话,那么要检查的串的长度必须大于等于原串的长度-1。
然后贪心判定,两串同时从头开始扫,如果原本串中的为\(*\)那么当前串中的剩下的大于原本串的长度并且为‘bad’则加入\(*\),否则跳过\(*\)继续匹配,\(?\)是很好处理的。
最后只要判断两串是否都能跑完。复杂度\(O(n)\)
#include<bits/stdc++.h>using namespace std;char str1[30];char str2[100005];char str3[100005];bool flag[30];bool f = false;bool check(int l);int main(void){ memset(flag,0,sizeof(flag)); scanf("%s",str1); scanf("%s",str2); for(int i = 0; i < strlen(str1); i++) flag[str1[i] - 'a']++; int n; scanf("%d",&n); int l = strlen(str2); for(int i = 0; i < l; i++) if(str2[i] == '*') f = true; while(n--) { scanf("%s",str3); if(check(l)) printf("YES\n"); else printf("NO\n"); } return 0;}bool check(int l){ if(!f) { if(strlen(str3) != l) return false; } else { if(strlen(str3) < l-1) return false; } //printf("1\n"); int z = 0; int k = strlen(str3); //int ff = 0; int i; for( i = 0; i < l&&z < k+1;) { if(str2[i] == '*'&&k-l >= 0) { //printf("111\n"); while(!flag[str3[z]-'a']&&z < k&&l-i < k-z+1) z++; } //printf("%d\n",z); if(str2[i] == '*') i++; if(i >= l||z >= k+1) break; if(str2[i] == '?') { if(!flag[str3[z]-'a']) return false; else { i++,z++; } } else { if(str2[i] != str3[z]) { //printf("%d %d\n",i,z); return false; } else i++,z++; } } if(f&&(i < l||z < k)) { return false; } return true;}
阅读全文
0 0
- B. Petya and Exam
- B. Petya and Exam【字符串】
- B. Petya and Exam(思维)
- CodeForces 832B Petya and Exam
- codeforces B. Petya and Exam (模拟)
- cf 832B Petya and Exam
- codeforces 832B (Petya and Exam)
- CodeForces 832 B. Petya and Exam
- CodeForces-832B Petya and Exam
- Codeforces832B Petya and Exam
- Petya and Exam
- cfRound425B. Petya and Exam
- Codeforces Round #425 (Div. 2)B. Petya and Exam
- CodeFroces 823B. Petya and Exam(构造题)
- Codeforces Round #425 (Div. 2) B. Petya and Exam
- codeforces 832B ——Petya and Exam
- (字符串匹配)Codeforces Round #425 B. Petya and Exam
- Codeforces Round #425 (Div. 2) B. Petya and Exam
- ROS学习(基于Ubuntu 15.04 和ROS Jade)第三章 ROS核心教程 之 14 测试简单的Service和Client
- unity 修改亮度,对比度,饱和度的处理
- JPEG2000中最佳截断嵌入码块编码EBCOT
- Oracle语句—日期格式
- Cookie与Session的区别-总结很好的文章
- B. Petya and Exam
- 鼠标移上图片缓慢上移
- 一个简单可用的nginx.conf配置
- 浅谈mvc,mvp,mvvm的理解(一)
- Jpush 极光推送java服务器端
- LeetCode
- Docker学习笔记(二)安装与卸载
- 授权模拟股票
- 企慧Q5快速开发平台系列之三:工作流设计