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;
}

B. Petya and Exam的更多相关文章

  1. CodeForces832-B. Petya and Exam

    补的若干年以前的题目,水题,太菜啦_(:з」∠)_    B. Petya and Exam time limit per test 2 seconds memory limit per test 2 ...

  2. 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 mem ...

  3. CodeForces 832B Petya and Exam

    B. Petya and Exam time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

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

  5. Codefroces 832B Petya and Exam

    B. Petya and Exam time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  6. E - Petya and Exam CodeForces - 832B 字典树+搜索

    E - Petya and Exam CodeForces - 832B 这个题目其实可以不用字典树写,但是因为之前写过poj的一个题目,意思和这个差不多,所以就用字典树写了一遍. 代码还是很好理解的 ...

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

  8. 832B Petya and Exam

    题意:给你两个串,第一个串里面的字母都是good 字母, 第二个串是模式串,里面除了字母还有?和*(只有一个) ?可以替换所有good字母, *可以替换所有坏字母和空格(可以是多个坏字母!!!这点卡了 ...

  9. CF832B Petya and Exam

    思路: 模拟. 实现: #include <iostream> using namespace std; string a, b; ]; bool solve() { ) return f ...

随机推荐

  1. 论文解读(SDNE)《Structural Deep Network Embedding》

    论文题目:<Structural Deep Network Embedding>发表时间:  KDD 2016 论文作者:  Aditya Grover;Aditya Grover; Ju ...

  2. C语言中的除法的计算

    不用除号,计算除法运算.思路是使用减法运算!思路1:循环采用减法每次减去n,直到做完减法之后结果小于0为止 但是这样次数较大  如求100/3,需要次数为34次. 思路2:循环采用减法每次减去k,K的 ...

  3. A Child's History of England.32

    And so, in darkness and in prison, many years, he thought of all his past life, of the time he had w ...

  4. Spark(三)【RDD中的自定义排序】

    在RDD中默认的算子sortBy,sortByKey只能真的值类型数据升序或者降序 现需要对自定义对象进行自定义排序. 一组Person对象 /** * Person 样例类 * @param nam ...

  5. Sharding-JDBC 实现水平分库分表

    1.需求分析

  6. CentOS7 搭建maven私服Nexus

    下载解压 官网https://www.sonatype.com/download-oss-sonatype 下载页面 https://help.sonatype.com/repomanager2/do ...

  7. Vue相关,Vue生命周期及对应的行为

    先来一张经典图 生命钩子函数 使用vue的朋友们知道,生命周期函数长这样- mounted: function() { } // 或者 mounted() { } 注意点,Vue的所有生命周期函数都是 ...

  8. Java_zip_多源文件压缩到指定目录下

    依赖: <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress --> <depend ...

  9. DOM解析xml学习笔记

    一.dom解析xml的优缺点 由于DOM的解析方式是将整个xml文件加载到内存中,转化为DOM树,因此程序可以访问DOM树的任何数据. 优点:灵活性强,速度快. 缺点:如果xml文件比较大比较复杂会占 ...

  10. my41_主从延迟大排查

    半同步复制 主库执行 INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so'; SET GLOBAL rpl_semi_sync ...