Codeforces Round #425 (Div. 2) B. Petya and Exam(暴力大法好)

题目链接:http://codeforces.com/problemset/problem/832/B

题意:给定一些小写字符(定义为好字符,除这些好字符外的其他小写字符都是坏字符)。和字符串S,S里面除了小写字母以外还有?字符,?可以用任何的好字符替代;*字符,*只可以用坏字符串

替代,然后给出n个字符串去匹配S字符,问是否能成功。

题解:QAQ,这道题写的我头发又掉了好多。

设拿来匹配S的字符串为T

字符串要匹配,长度要相同吧。*既然这么强大,可以空,那么T的长度最多比S的长度少1;然后不管T长度有多少,只要*符号存在的位置合理,那么就可能匹配。

然后就是匹配了,两个方向 向*位置走在,在这个过程中要保证?的时候T的都是好字符,不是?的时候两者相同。然后就是*代表的那串字符串里面都要是坏字符,

判断一下就可以了。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5
 6 int num[30];
 7
 8 int main(){
 9     ios_base::sync_with_stdio(false);
10     int n;
11     string S,T,Q;
12     cin>>T>>S;
13     cin>>n;
14     memset(num,0,sizeof(num));
15     for(int i=0;i<T.size();i++) num[T[i]-96]=1;
16     int Slen=S.size(),flag=0;
17     for(int i=0;i<Slen;i++) if(S[i]==‘*‘) flag=1;
18
19     for(int k=0;k<n;k++){
20         cin>>Q;
21         int idx=1,Qlen=Q.size();
22
23         if(Qlen<Slen-flag) {cout<<"NO"<<endl;continue;}
24         if(Qlen!=Slen&&!flag) {cout<<"NO"<<endl;continue;}
25
26         int s=0,t=1;
27         while(S[s]!=‘*‘&&s<Slen&&idx){
28             if(S[s]==‘?‘&&num[Q[s]-96]==0) idx=0;
29             else if(S[s]!=‘?‘&&S[s]!=Q[s]) idx=0;
30             s++;
31         }
32
33         while(S[Slen-t]!=‘*‘&&idx&&Slen-t>=0&&Qlen-t>=0){
34             if(S[Slen-t]==‘?‘&&num[Q[Qlen-t]-96]==0) idx=0;
35             else if(S[Slen-t]!=‘?‘&&S[Slen-t]!=Q[Qlen-t]) idx=0;
36             t++;
37         }
38
39         for(int i=s;i<=Qlen-t&&flag;i++){
40             if(num[Q[i]-96]==1)
41             idx=0;
42         }
43         if(idx) cout<<"YES"<<endl;
44         else cout<<"NO"<<endl;
45     }
46
47     return 0;
48 }
时间: 2024-12-25 16:39:41

Codeforces Round #425 (Div. 2) B. Petya and Exam(暴力大法好)的相关文章

Codeforces Round #425 (Div.2)

A.Sasha and Sticks 题意:有两个人,每次轮流从n个棒子中抽出k个,直到剩余不足k个棒子.Sasha先抽.如果Sasha抽取的多,则输出YES,否则输出NO. 思路:n/k为奇数时,Sasha获胜. 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 typedef long long LL; 5 LL n, k; 6 int main() 7 { 8 while (~scanf(&quo

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 connected with n - 1 routes so that each route connects two stations, and it is possible to reach every station from any other. The boys decided to h

Codeforces Round #425 (Div. 2) Problem C (Codeforces 832C) Strange Radiation - 二分答案 - 数论

n people are standing on a coordinate axis in points with positive integer coordinates strictly less than 106. For each person we know in which direction (left or right) he is facing, and his maximum speed. You can put a bomb in some point with non-n

Codeforces Round #425 (Div. 2) Problem A Sasha and Sticks

It's one more school day now. Sasha doesn't like classes and is always bored at them. So, each day he invents some game and plays in it alone or with friends. Today he invented one simple game to play with Lena, with whom he shares a desk. The rules

Codeforces Round #510 (Div. 2) D. Petya and Array (权值线段树)

题目地址:http://codeforces.com/contest/1042/problem/D 题意:给你n个数,问有多少个区间的和的值小于t 分析:区间和问题,常常用到前缀和来进行预处理,所以先预处理出前缀和数组sum sum[i]代表前i个数的和,那么sum[i]的贡献就是,  当i<k<=n时,存在多少个k,使sum[k]<t+sum[i] 也就是求在[i+1,n]中,小于t+sum[i]的数有多少. 所以我们可以类比于询问一个数是区间第几大的方法,使用权值线段树来解决,这里因

【树链剖分】【dfs序】【LCA】【分类讨论】Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground

一棵树,q次询问,每次给你三个点a b c,让你把它们选做s f t,问你把s到f +1后,询问f到t的和,然后可能的最大值是多少. 最无脑的想法是链剖线段树--但是会TLE. LCT一样无脑,但是少一个log,可以过. 正解是分类讨论, 如果t不在lca(s,f)的子树内,答案是dis(lca(s,f),f). 如果t在lca(s,f)的子树内,并且dep(lca(s,t))>dep(lca(f,t)),答案是dis(lca(s,t),f): 否则答案是dis(lca(f,t),f). #in

Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground

题意:给出 一颗树,然后q个询问,每个询问给出3个点,让我们自己选择一个起点一个终点,这条路线上的点标记,问第三个点到终点的最多标记点是多少 思路:第三个点到终点的标记点,肯定是第三个点与起点的最近公共祖先到终点的标记点.我们可以求出三个点的的三种最近公共祖先,取深度最大的点K,然后求K到三个点的距离+1 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=1e5+10; 4 5 const int DEG=17; 6

Codeforces Round #425 (Div. 2) D 树链剖分 + 树状数组维护区间

一看就知道 可以LCA判断做 也可以树链剖分拿头暴力 然而快速读入和线段树维护区间会T70 于是只能LCA? 线段树的常数不小 于是需要另外一种办法来进行区间加减和查询区间和 就是使用树状数组 这个题的代码 #include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #include<vector> #include<cstring> using na

Codeforces Round #540 (Div. 3) C. Palindromic Matrix 【暴力】

任意门:http://codeforces.com/contest/1118/problem/C C. Palindromic Matrix time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Let's call some square matrix with integer values in its cells palind