E - Petya and Exam

CodeForces - 832B

这个题目其实可以不用字典树写,但是因为之前写过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 字典树+搜索的更多相关文章

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

  2. Watto and Mechanism CodeForces - 514C (字典树,哈希)

    大意: 给定字符串集$S$, 每次询问给出字符串$a$, 求$S$中是否存在一个字符串恰好与$a$相差一个字符. 直接建字典树暴力复杂度是$O(n\sqrt{n})$, 也可以用set维护所有哈希值, ...

  3. Vitya and Strange Lesson CodeForces - 842D 字典树+交换节点

    题意: Today at the lesson Vitya learned a very interesting function - mex. Mex of a sequence of number ...

  4. SPOJ Hacking(字典树 + 搜索)题解

    思路1:字典树存每个串,然后dfs遍历是否存在.这里有个技巧,如果每次都重新初始化字典树为-1,那么会超时,所以我先初始化为-1,然后设一个Case,每个test时Case都++,那么只要开一个数组判 ...

  5. Petya and Array CodeForces - 1042D (树状数组)

    D. Petya and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. CodeForces 832B Petya and Exam

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

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

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

  9. codeforces 706D (字典树)

    题目链接:http://codeforces.com/problemset/problem/706/D 题意:q次操作,可以向多重集中增添,删除,询问异或最大值. 思路:转化为二进制用字典树存储,数字 ...

随机推荐

  1. editplus 怎么替换为换行

    到editplus 的搜索 菜单中,选择替换,记住 这边如果是简单的一些 通用字符 替换可以直接替换,如果是一些特殊的字符 那必须选择 替换框左下中间的 “正则表达式”,即把这个“正则表达式” 前边的 ...

  2. GitHub 热点速览 Vol.16:化身蒙娜丽莎和乔布斯对话

    摘要:妙趣横生,上周的 GitHub 热点的关键词.无论是让你化身为爱因斯坦开启会议脑暴模式 avatarify,还是和上周人人都是抠图师项目的同门项目 3D 照片修复:3d-photo-inpain ...

  3. D. Minimax Problem Codeforces 1288D binary_search+二进制

    题目大意:n*m的矩阵中,找到两行数,可以形成两个一维数组,数组1的位置i和数组2的位置i去最大构成新数组b的元素b[i],最终目的要使数组b中最小的数尽可能的大 题解: m的范围是(1,8),比较小 ...

  4. BUUOJ [极客大挑战 2019]Secret File

    [极客大挑战 2019]Secret File 0X01考点 php的file伪协议读取文件 ?file=php://filter/convert.base64-encode/resource= 0X ...

  5. HTML+CSS教程(一)简介及其基本标签的使用方法

    一.前端 HTML(结构):HyPer TEXT Markup LanguageCSS(样式): 样式就是对于结构的一种美化JavaScript(js: 行为/ 提供了用户和界面的交互方式)jQuer ...

  6. 【已解决】error setting certificate verify locations报错

    目录 1.问题描述 2.问题分析 3.解决方法 1.问题描述 在公司的电脑上从Github上clone项目的时候git黑窗口报错"error setting certificate veri ...

  7. Ubuntu16.04 安装eclipse

    首先确保自己的Ubuntu已经安装了jdk并且配置好了环境变量 然后在官网下载相应的eclipse安装包: https://www.eclipse.org/downloads/packages/ 下载 ...

  8. 使用STM8S i2c对TPS65987寄存器进行读写

    上图是TPS65987的i2c读写协议,和标准i2c协议有点出入,不过也不难理解,在读的时候i2c slave在发送数据过来之前会先发送1byte数据表示后面会有几个字节数据过来,在写的时候i2c h ...

  9. token认证和理解

    认知篇:https://blog.csdn.net/FYGu18/article/details/89345490 token失效篇认知:https://segmentfault.com/q/1010 ...

  10. MySQL数据库缓存操作

    安装: 启动的话: -d:以后台的方式进行: -l:选择监听指定的ip服务地址:-m:给他分配多大的内存:-p:端口号默认的端口为11211的服务端口: 另一个: 安装:telnet 这个可以用来测试 ...