Codeforces 514C Watto and Mechanism(字典树)
题目链接 Watto and Mechanism
题意 给出$n$个串(相当于字典),然后给出$m$个询问。
每个询问以字符串的形式给出,你需要改变这个字符串中的任意一个字符
(必须改变且只能改变一个)
如果改变之后可以成为$n$个串中的一个字符串,则输出$YES$, 否则输出$NO$。
字母集合为$\left\{ a, b, c \right\}$
考虑字典树。
首先把$n$个单词插入字典树中。
询问的时候用$dfs$,$flag$表示搜索到当前是否已经改变过一个字符。
如果已经改变过那只能按照当前剩下的字符串一条路查询下去。
否则可以按老字符或新字符进行查询。
(这题很卡内存)
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) const int N = 2e7 + 3; char s[600010];
int n, q, ans, len, tot = 0;
int ch[N][3];
bitset <N> cnt; int newnode(){
++tot;
rep(i, 0, 2) ch[tot][i] = 0;
cnt[tot] = 0;
return tot;
} void insert(char* s){
int now = 0;
for (int i = 0; s[i]; ++i){
int w = s[i] - 'a';
if (!ch[now][w]) ch[now][w] = newnode();
now = ch[now][w];
}
cnt[now] = 1;
} void dfs(int x, int flag, int now){
if (ans) return;
if (x == len && flag && cnt[now]){ ans = 1; return;}
if (x >= len) return;
if (flag){
int w = s[x] - 'a';
if (ch[now][w]) dfs(x + 1, flag, ch[now][w]);
} else{
rep(i, 0, 2)
if (i != s[x] - 'a' && x + 1 == len && ch[now][i] && cnt[ch[now][i]]) ans = 1; rep(i, 0, 2) if (i != s[x] - 'a' && ch[now][i]){
dfs(x + 1, 1, ch[now][i]);
} int w = s[x] - 'a';
if (ch[now][w]){
dfs(x + 1, flag, ch[now][w]);
} }
} int main(){ scanf("%d%d", &n, &q);
rep(i, 1, n){
scanf("%s", s);
insert(s);
} rep(i, 1, q){
scanf("%s", s);
len = strlen(s);
ans = 0;
dfs(0, 0, 0);
puts(ans ? "YES" : "NO");
} return 0;
}
Codeforces 514C Watto and Mechanism(字典树)的更多相关文章
- 【codeforces 514C】Watto and Mechanism(字典树做法)
[题目链接]:http://codeforces.com/contest/514/problem/C [题意] 给你n个字符串; 然后给你m个询问;->m个字符串 对于每一个询问字符串 你需要在 ...
- Codeforces Round #291 (Div. 2) C. Watto and Mechanism [字典树]
传送门 C. Watto and Mechanism time limit per test 3 seconds memory limit per test 256 megabytes input s ...
- CF Watto and Mechanism (字典树+深搜)
Watto and Mechanism time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 514C Watto and Mechanism 【Trie树】+【DFS】
<题目链接> 题目大意:输入n个单词构成单词库,然后进行m次查询,每次查询输入一个单词(注意这些单词只由a,b,c构成),问该单词库中是否存在与当前查询的单词有且仅有一个字符不同的单词. ...
- codeforces gym #101161F-Dictionary Game(字典树+树上删边游戏)
题目链接: http://codeforces.com/gym/101161/attachments 题意: 给一个可以变化的字典树 在字典树上删边 如果某条边和根节点不连通那么这条边也删除 谁没得删 ...
- CodeForces 706D Vasiliy's Multiset (字典树查询+贪心)
题意:最开始的时候有一个集合,集合里面只有一个元素0,现在有q次操作,操作分为3种: + x: 表示向集合中添加一个元素x - x:表示删除集合中值为x的一个元素 ? x:表示查询集合中与x异或的最大 ...
- Codeforces 577E Ann and Half-Palindrome 字典树
题目链接 题意: 若一个字符串是半回文串.则满足第一位和最后一位相等, 第三位和倒数第三位相等.如此类推. 给定一个字符串s,输出s的全部子串中的半回文串字典序第k大的 字符串. good[i][j] ...
- Watto and Mechanism CodeForces - 514C (字典树,哈希)
大意: 给定字符串集$S$, 每次询问给出字符串$a$, 求$S$中是否存在一个字符串恰好与$a$相差一个字符. 直接建字典树暴力复杂度是$O(n\sqrt{n})$, 也可以用set维护所有哈希值, ...
- codeforces 706D (字典树)
题目链接:http://codeforces.com/problemset/problem/706/D 题意:q次操作,可以向多重集中增添,删除,询问异或最大值. 思路:转化为二进制用字典树存储,数字 ...
随机推荐
- linx vim 文件操作 ubuntu server 软件源
mv /etc/danted.conf /etc/danted.conf.bak sudo wget https://files.cnblogs.com/files/marklove/danted.t ...
- Python基础篇 -- if while 语句
2.7 if语句 # 单纯if if 条件: 代码块 当条件成立,执行代码块 # 二选一 if 条件: 代码块1 else: 代码块2 #当条件为真,执行代码块1,否则执行代码块2 # 多选一 没有e ...
- 人脸识别源代码Open cv
#include <stdio.h> #include <string.h> #include "cv.h" #include "cvaux.h& ...
- Mac 录制视频,并转为GIF格式
内容中包含 base64string 图片造成字符过多,拒绝显示
- 如何用纯 CSS 创作一个雷达扫描动画
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/VdbGvr 可交互视频 ...
- python 有4个数字1234,能组成多少个互不相同且无重复的三位数数字。
def output(): count = 0 for i in range(1,5): for j in range(1, 5): for k in range(1, 5): if i==j or ...
- Python GUI界面开发环境配置:Pycharm+PyQt5
通过DoS命令行执行如下命令,可能需要管理员权限. 检查Python版本:python 更新pip版本:python -m pip install --upgrade pip 安装PyQt5: pip ...
- requests库的学习——跟随官方文档
发送GET请求: import requests r=requests.get("http://www.kekenet.com/") 如果需要传递参数可以有以下几种方法: impo ...
- 20130829ios cocos2d下拉列表的向上弹出实现(ios开发遇到的frame的问题)
前几天仔细区分了ios中frame,bounds,center之间的关系. Frame:边框矩形,是视图相对于其父坐标的位置和大小 Bounds:边界矩形,是本地坐标系统(一般较少使用) Center ...
- WIN10配置MAVEN
添加新的系统环境变量M2_HOME, 并设置其值为你安装的目录MAVEN_HOME=D:\Softwares\apache-maven-3.2.2. 更新系统PATH 变量, 添加;%M2_HOME% ...