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. TensorFlow保存、加载模型参数 | 原理描述及踩坑经验总结

    写在前面 我之前使用的LSTM计算单元是根据其前向传播的计算公式手动实现的,这两天想要和TensorFlow自带的tf.nn.rnn_cell.BasicLSTMCell()比较一下,看看哪个训练速度 ...

  2. 新版本Xcode 6的视图调试详解

    开发者会经常遇到视图或者Auto Layout约束中存在bug的情况,并且这种bug很难通过代码发现,所以开发者很有必要熟知如何进行简单高效的视图调试,而Xcode 6的发布使得视图调试变得前所未有的 ...

  3. IE各版本CSS Hack(兼容性处理)语法速查表

    为了兼容IE各个版本,需要在CSS中添加额外的代码,比如以前常用的_width.之所以工作,是因为浏览器会忽略不能解析的样式规则,因此举个例子来说,把_width写在width下面,对于非IE浏览器会 ...

  4. F - Dragon Balls

    Five hundred years later, the number of dragon balls will increase unexpectedly, so it's too difficu ...

  5. 如何将SqlServer配置为django的数据源(2.2以后版本)

    django-pyodbc-azure 是一个官方推荐的 第三方django数据库支持backend. 根据官网的介绍django-pyodbc-azure 只能支持到 django 2.1.如果涉及 ...

  6. C#开发BIMFACE系列34 服务端API之模型对比5:获取模型构件对比差异

    系列目录     [已更新最新开发文章,点击查看详细] BIMFACE平台提供了服务端“获取修改构件属性差异”API,其返回的结果也是一个列表,仅针对修改的构件(不包含新增.删除的构件),是指对于一个 ...

  7. React Hooks: useCallback理解

    useCallback把匿名回调“存”起来 避免在component render时候声明匿名方法,因为这些匿名方法会被反复重新声明而无法被多次利用,然后容易造成component反复不必要的渲染. ...

  8. orcale 多列转一行显示

    强大的数据库有个自带函数wm_concat() wm_concat()这个函数放的是需要汇总的列 select wm_concat(name) name  from tablename

  9. linux上Docker安装gogs私服亲测(详解)

    一.前言 有网友问我为什么要使用私服,可能大部分人都不是太懂,网上那么多存储仓库而且好用方便,但是你想过没有如果企业中的项目,放在人家的仓库上这个安全性不是太好,所以说一般企业都会有自己的私服.本章教 ...

  10. swoole--服务平滑重启

    参考来源:https://wiki.swoole.com/wiki/page/p-server/reload.html shell代码: echo "loading..." pid ...