E - Petya and Exam CodeForces - 832B 字典树+搜索
E - Petya and Exam
这个题目其实可以不用字典树写,但是因为之前写过poj的一个题目,意思和这个差不多,所以就用字典树写了一遍。
代码还是很好理解的,主要就是哪个findx函数,这个要好好理解。
如果碰到*或着?就要重新标记一下,其他都是一样的,对于?可以跳过好字母,对于*可以跳过若干个不好的字母,
这个就在直接跳过之前加一个判断条件就可以了。
贴一下代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std;
const int maxn = 1e5 + ;
typedef long long ll;
int tree[maxn][], tot = , ans, isgood[];
bool flag[maxn];
char good[], s[maxn], str[maxn];
int add(char *s) {
int root = , id, len = strlen(s);
for (int i = ; i < len; i++) {
if (s[i] == '*') id = ;
else if (s[i] == '?') id = ;
else id = s[i] - 'a';
if (!tree[root][id]) tree[root][id] = ++tot;
root = tree[root][id];
// printf("root=%d s=%c\n", root, s[i]);
}
flag[root] = true;
return root;
} void findx(char *s, int root, int pos) {
// printf("%s root=%d pos=%d\n", s, root, pos);
int len = strlen(s);
if (pos > len) return;
if (len == pos && flag[root]) {
ans = ;
return;
}
if (tree[root][]) {
int flag = ;
int len1 = strlen(str) - ;
int ends = len1 - pos;
// printf("ends=%d len-ends=%d pos=%d\n", ends, len - ends-1, pos);
for (int i = pos; i <= len - ends - ; i++) {
int id = s[i] - 'a';
if (isgood[id]) {
flag = ;
break;
}
}
// printf("flag=%d pos=%d len-ends=%d\n", flag, pos, len - ends);
if (flag&&len - ends >= pos) findx(s, tree[root][], len - ends);
// for (int j = pos; j <= len; j++) {
// int id = s[j] - 'a';
// if (isgood[id]) break;
// findx(s, tree[root][26], j + 1);
// }
}
if (tree[root][]) {
int length = strlen(good);
for (int i = ; i < length; i++) {
if (s[pos] == good[i]) findx(s, tree[root][], pos + );
}
}
int id = s[pos] - 'a';
if (s[pos] <= 'z'&&s[pos] >= 'a'&&tree[root][id]) findx(s, tree[root][id], pos + );
} int main() {
scanf("%s%s", good, str);
int len = strlen(good);
for (int i = ; i < len; i++) {
int id = good[i] - 'a';
isgood[id] = ;
}
int num = add(str);
int m;
scanf("%d", &m);
while (m--) {
scanf("%s", s);
ans = ;
findx(s, , );
if (ans) printf("YES\n");
else printf("NO\n");
}
return ;
}
字典树
Wild Words poj 1816
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std;
const int maxn = 2e6 + ;
typedef long long ll;
int tree[maxn][], tot = , ans, p[maxn];
bool flag[maxn], vis[maxn];
char s[maxn], str[maxn];
int add(char *s) {
int root = , id, len = strlen(s);
for (int i = ; i < len; i++) {
if (s[i] == '*') id = ;
else if (s[i] == '?') id = ;
else id = s[i] - 'a';
if (!tree[root][id]) tree[root][id] = ++tot;
root = tree[root][id];
}
flag[root] = true;
return root;
} void findx(char *s, int root, int pos) {
// printf("%s root=%d pos=%d\n", s, root, pos);
int len = strlen(s);
if (pos > len) return;
if (len == pos && flag[root]) {
vis[root] = ;
}
if (tree[root][]) {
for (int i = pos; i <= len; i++) findx(s, tree[root][], i);
}
if (tree[root][]) findx(s, tree[root][], pos + );
int id = s[pos] - 'a';
if (s[pos] <= 'z'&&s[pos] >= 'a'&&tree[root][id]) findx(s, tree[root][id], pos + );
}
vector<int>res;
int main() {
int n, m;
scanf("%d%d", &n, &m);
for(int i=;i<=n;i++)
{
scanf("%s", str);
p[i]=add(str);
}
while(m--)
{
scanf("%s", str);
findx(str, , );
res.clear();
for(int i=;i<=n;i++) if (vis[p[i]]) res.push_back(i - );
int len = res.size();
if (len == ) printf("Not match\n");
else {
for (int i = ; i < len - ; i++) printf("%d ", res[i]);
printf("%d\n", res[len - ]);
}
for (int i = ; i <= n; i++) vis[p[i]] = ;
}
return ;
}
E - Petya and Exam CodeForces - 832B 字典树+搜索的更多相关文章
- 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 ...
- Watto and Mechanism CodeForces - 514C (字典树,哈希)
大意: 给定字符串集$S$, 每次询问给出字符串$a$, 求$S$中是否存在一个字符串恰好与$a$相差一个字符. 直接建字典树暴力复杂度是$O(n\sqrt{n})$, 也可以用set维护所有哈希值, ...
- Vitya and Strange Lesson CodeForces - 842D 字典树+交换节点
题意: Today at the lesson Vitya learned a very interesting function - mex. Mex of a sequence of number ...
- SPOJ Hacking(字典树 + 搜索)题解
思路1:字典树存每个串,然后dfs遍历是否存在.这里有个技巧,如果每次都重新初始化字典树为-1,那么会超时,所以我先初始化为-1,然后设一个Case,每个test时Case都++,那么只要开一个数组判 ...
- Petya and Array CodeForces - 1042D (树状数组)
D. Petya and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- CodeForces 832B Petya and Exam
B. Petya and Exam time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #311 (Div. 2) E. Ann and Half-Palindrome 字典树/半回文串
E. Ann and Half-Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
- Codeforces Round #311 (Div. 2) E - Ann and Half-Palindrome(字典树+dp)
E. Ann and Half-Palindrome time limit per test 1.5 seconds memory limit per test 512 megabytes input ...
- codeforces 706D (字典树)
题目链接:http://codeforces.com/problemset/problem/706/D 题意:q次操作,可以向多重集中增添,删除,询问异或最大值. 思路:转化为二进制用字典树存储,数字 ...
随机推荐
- tf.nn.dropout 激活函数
tf.nn.dropout(x,keep_prob,noise_shape=None,seed=None,name=None) 参数: x:一个浮点型Tensor. keep_prob:一个标量Ten ...
- Python财经数据接口包TuShare的使用
安装TuShare 方式1:pip install tushare 方式2:访问https://pypi.python.org/pypi/tushare/下载安装 方式3:将源代码下载到本地pytho ...
- GeiGebra指令
- 五分钟!用python绘制漂亮的系统架构图
Diagrams 是一个基于Python绘制云系统架构的模块,它能够通过非常简单的描述就能可视化架构,并支持以下6个云产品的图标: AWS.Azure.GCP.K8s.阿里云 和 Oracle 云 基 ...
- 好用的mitmproxy代理抓包
安装证书 浏览器输入 `mitm.it` 下载证书有时候打不开,可能是起的服务卡死了,回车下命令行,再再网页刷新下载证书就可以了. mitmweb Chrome浏览器代理设置 打开的话,记得保存点一下 ...
- Java封装 概述
封装:是指隐藏对象的属性和实现细节,仅对外提供公共访问方式.好处:隐藏实现细节,提供公共的访问方式提高了代码的复用性提高安全性 封装原则:将不需要对外提供的内容都隐藏起来把属性隐藏,提供公共方法对其访 ...
- 如何用Github钩子做自动部署
最近机缘巧合的购置了域名和服务器,不用实在是浪费,再加上一直没有属于自己的个人网站,所以打算用hexo在服务器上玩一下,这样也就不用再纠结用Github pages还是Gitee pages了.当然, ...
- Java中的字符串操作(比较String,StringBuiler和StringBuffer)
一.前言 刚开始学习Java时,作为只会C语言的小白,就为其中的字符串操作而感到震撼.相比之下,C语言在字节数组中保存一个结尾的\0去表示字符串,想实现字符串拼接,还需要调用strcpy库函数或者自己 ...
- ES6让字符串String增加了哪些好玩的特性呢?
确实因为现在天气变热了,所以一天天的这么写我也很累.所以如果阅读的时候有什么错误还请大家指出来,不好意思.学习永无止境. OK,今天继续讲解ES6系列知识 学过上一节的解构赋值就知道,ES6确实给我们 ...
- [Python进阶].pyc的那点事
1. 什么是 .pyc文件 .pyc文件 就是 Python的字节码(byte-compiled)文件..py文件运行时,python会自动将其编译成PyCodeObject并写入.pyc文件,再有p ...