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次操作,可以向多重集中增添,删除,询问异或最大值. 思路:转化为二进制用字典树存储,数字 ...
随机推荐
- 使用snapjs实现svg路径描边动画
一,snap.svg插件在近几天,突然接到一个需求,内容是要在网页上写一个路径的动画,还需要可以随意控制动画的速度,开始于结束,本来是一个图片可以解决的问题,结果就这样变难了呀,在网上查一会之后,突然 ...
- C - League of Leesins
乱搞一发,,竟然过了!!! 题目大意:输入一个整数n,然后n-2行,每一行3个数字,表示一个数组中连续的3个数字,然后将这3个数字的顺序打乱,然后再将这个n-2行打乱,要求还原数组. 题解:先找到前3 ...
- 最短路径变形 POJ 2253
Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sit ...
- Eclipse Hadoop源码阅读环境
一.解压hadoop src包到workspace目录.为加快下载jar包的速度,在eclipse的maven设置里将配置文件的路径设置正确,然后配置maven的settings.xml: <m ...
- Mark down 使用总结
Markdown语法 Markdown是一种纯文本.轻量级的标记语言,通过简单的标记,就可以使文本具有一定的格式,操作简单.使用广泛,常见的比如github上的README.md . Markdown ...
- @SessionAttributes 的使用
@SessionAttributes 注解只用作用在 类 上,作用是将指定的 Model 的键值对保存在 session 中.可以让其他请求共用 session 中的键值对. 指定保存的属性名 作用是 ...
- C#开发BIMFACE系列31 服务端API之模型对比2:获取模型对比状态
系列目录 [已更新最新开发文章,点击查看详细] 在上一篇<C#开发BIMFACE系列30 服务端API之模型对比1:发起模型对比>中发起了2个模型对比,由于模型对比是在BIMFAC ...
- 浅析 CSS 中的边距重叠
浅析 CSS 中的边距重叠 边距重叠是什么 在说边距重叠之前,先以正常的思维来考虑如果你现在是浏览器引擎遇到这种情况应该怎么办? 现在有两个元素 div1 和 div2 紧挨着,中间没有它元素,它们的 ...
- 你知道什么是 GitHub Action 么?
本文是 GitHub Action 的入门教程,如您已有相关使用经验可以直接关掉. GitHub Action 是 GitHub 于 2018 年 10 月推出的一个 CI\CD 服务. 之前一直都是 ...
- System类的两个静态方法currentTimeMillis 和 arraycopy
package com.yhqtv.demo02.ThreadPool; import java.util.Arrays; public class Test { public static void ...