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次操作,可以向多重集中增添,删除,询问异或最大值. 思路:转化为二进制用字典树存储,数字 ...
随机推荐
- [译]HAL-超文本应用语言
[译]HAL-超文本应用语言 精益超媒体类型 总结 HAL 是一种简单的格式,它提供了一种一致且简便的方法在 API 的资源之间进行超链接. 采用 HAL 将使您的 API 易于探索,并且其文档很容易 ...
- 一个不错的spring 学习博客
http://www.iteye.com/blogs/subjects/spring-tittle-tattle
- 解决从服务器获取的数组是 __NSCFConstantString以及""没有空格字符串的问题
AJ分享,必须精品 问题 项目遇到了个bug,从服务器获取到的数据是这样的 { status = 1, data = [ { uid = 161, type = 2, id = 79, addtime ...
- AJ学IOS(36)UI之手势事件旋转_缩放_拖拽
AJ分享,必须精品 效果 完成一个图片的捏合缩放,拖拽,旋转动作. 设计思路 拖拽: 首先是最简单的拖拽 //拖拽 -(void)panTest { UIPanGestureRecognizer *p ...
- FJUT2019暑假第二次周赛题解
A 服务器维护 题目大意: 给出时间段[S,E],这段时间需要人维护服务器,给出n个小时间段[ai,bi],代表每个人会维护的时间段,每个人维护这段时间有一个花费,现在问题就是维护服务器[S,E]这段 ...
- 【Java】 语言基础习题汇总 [1] 基础概念到数组
1 JDK JRE JVM 三种之间的关系,以及JDK JRE 包含的主要结构有哪些? JDK = JRE + 开发工具 javac.exe java.exe javadoc.exe等等 JRE = ...
- JMeter分布式压测实战(2020年清明假期学习笔记)
一.常用压力测试工具对比 简介:目前用的常用测试工具对比 1.loadrunner 性能稳定,压测结果及颗粒度大,可以自定义脚本进行压测,但是太过于重大,功能比较繁多. 2.Apache ab(单接口 ...
- mac上搭建mysql环境配置和Navicat连接mysql
mac上搭建mysql环境配置 1.下载mysql for mac: https://downloads.mysql.com/archives/community/ 注意:mysql版本要和你的MAC ...
- Spiking-YOLO : 前沿性研究,脉冲神经网络在目标检测的首次尝试 | AAAI 2020
论文提出Spiking-YOLO,是脉冲神经网络在目标检测领域的首次成功尝试,实现了与卷积神经网络相当的性能,而能源消耗极低.论文内容新颖,比较前沿,推荐给大家阅读 来源:晓飞的算法工程笔记 公众 ...
- 推荐web前端框架bootstrap
bootstrap是基于Jquery而开发的一个前端框架. 全中文的学习网站:http://www.runoob.com/bootstrap/bootstrap-tutorial.html 实际上就是 ...