CF Watto and Mechanism (字典树+深搜)
3 seconds
256 megabytes
standard input
standard output
Watto, the owner of a spare parts store, has recently got an order for the mechanism that can process strings in a certain way. Initially the memory of the mechanism is filled with n strings. Then the mechanism should be able to process queries of the following type: "Given string s, determine if the memory of the mechanism contains string t that consists of the same number of characters as s and differs from s in exactly one position".
Watto has already compiled the mechanism, all that's left is to write a program for it and check it on the data consisting of n initial lines and m queries. He decided to entrust this job to you.
The first line contains two non-negative numbers n and m (0 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) — the number of the initial strings and the number of queries, respectively.
Next follow n non-empty strings that are uploaded to the memory of the mechanism.
Next follow m non-empty strings that are the queries to the mechanism.
The total length of lines in the input doesn't exceed 6·105. Each line consists only of letters 'a', 'b', 'c'.
For each query print on a single line "YES" (without the quotes), if the memory of the mechanism contains the required string, otherwise print "NO" (without the quotes).
- 2 3
aaaaa
acacaca
aabaa
ccacacc
caaac
- YES
NO
NO- 同样的思路,错了很多次,看来实现能力还是不够强。
字典树加搜索,没什么好说的,注意必须要改变一个字母就行。
- #include <bits/stdc++.h>
- using namespace std;
- const int SIZE = ;
- struct Node
- {
- Node * next[];
- bool word;
- }* ROOT = nullptr;
- char S[SIZE];
- bool dfs(char *,Node *,bool);
- int main(void)
- {
- int n,m;
- char ch;
- ROOT = new Node;
- for(int i = ;i < ;i ++)
- {
- ROOT -> next[i] = nullptr;
- ROOT -> word = false;
- }
- cin >> n >> m;
- cin.ignore();
- for(int i = ;i < n;i ++)
- {
- Node * cur = ROOT;
- while(cin.get(ch) && ch >= 'a' && ch <= 'c')
- {
- if(cur -> next[ch - 'a'])
- cur = cur -> next[ch - 'a'];
- else
- {
- cur -> next[ch - 'a'] = new Node;
- cur = cur -> next[ch - 'a'];
- for(int i = ;i < ;i ++)
- cur -> next[i] = nullptr;
- cur -> word = false;
- }
- }
- cur -> word = true;
- }
- for(int i = ;i < m;i ++)
- {
- cin >> S;
- if(dfs(S,ROOT,true))
- puts("YES");
- else
- puts("NO");
- }
- return ;
- }
- bool dfs(char * s,Node * t,bool chance)
- {
- if(!t)
- return false;
- if(!(*s))
- {
- if(t -> word && !chance)
- return true;
- return false;
- }
- if(dfs(s + ,t -> next[*s - 'a'],chance))
- return true;
- if(chance)
- for(int i = ;i < ;i ++)
- if(i != (*s - 'a'))
- if(dfs(s + ,t -> next[i],false))
- return true;
- return false;
- }
CF Watto and Mechanism (字典树+深搜)的更多相关文章
- 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 ...
- 【codeforces 514C】Watto and Mechanism(字典树做法)
[题目链接]:http://codeforces.com/contest/514/problem/C [题意] 给你n个字符串; 然后给你m个询问;->m个字符串 对于每一个询问字符串 你需要在 ...
- HDU-5423 Rikka with Tree。树深搜
Rikka with Tree 题意:给出树的定义,给出树相似的定义和不同的定义,然后给出一棵树,求是否存在一颗树即和其相似又与其不同.存在输出NO,不存在输出YES. 思路:以1号节点为根节点,我们 ...
- Codeforces 514C Watto and Mechanism(字典树)
题目链接 Watto and Mechanism 题意 给出$n$个串(相当于字典),然后给出$m$个询问. 每个询问以字符串的形式给出,你需要改变这个字符串中的任意一个字符 (必须改变且只能改变 ...
- Watto and Mechanism CodeForces - 514C (字典树,哈希)
大意: 给定字符串集$S$, 每次询问给出字符串$a$, 求$S$中是否存在一个字符串恰好与$a$相差一个字符. 直接建字典树暴力复杂度是$O(n\sqrt{n})$, 也可以用set维护所有哈希值, ...
- 【离线】【深搜】【树】Codeforces 707D Persistent Bookcase
题目链接: http://codeforces.com/problemset/problem/707/D 题目大意: 一个N*M的书架,支持4种操作 1.把(x,y)变为有书. 2.把(x,y)变为没 ...
- CodeM美团点评编程大赛初赛B轮 黑白树【DFS深搜+暴力】
[编程题] 黑白树 时间限制:1秒 空间限制:32768K 一棵n个点的有根树,1号点为根,相邻的两个节点之间的距离为1.树上每个节点i对应一个值k[i].每个点都有一个颜色,初始的时候所有点都是白色 ...
- 字典树+博弈 CF 455B A Lot of Games(接龙游戏)
题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...
- nyoj 230/poj 2513 彩色棒 并查集+字典树+欧拉回路
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=230 题意:给你许许多多的木棍,没条木棍两端有两种颜色,问你在将木棍相连时,接触的端点颜色 ...
随机推荐
- lipo命令
工作中,xcode工程遇到一个bug file was built for archive which is not the architecture being linked armv7 找了一些资 ...
- 第二百八十四天 how can I 坚持
又是一个周一.今天感觉过得好艰辛啊,幸好晚上程秀通过生日请客,吃了顿大餐,还拿回了一瓶酒.哈哈. 其他也没什么了.晚上玩的挺好.不过,回来,老是渴,一直想喝水,现在是又困,又累啊,睡觉了.
- 教程-在F9后提示内存错误,点击了乎略,之后怎么取消乎略?
问题现象:F9后,调试程序,提示内存错误,点击了“乎略”.之后再也没有出现错误了.可是想改这个BUG时,没法取消乎略了. 问题原因:在DLEPHI的选项中是这么一个地方是可以设置的. 问题处理:打开D ...
- stm32f407 定时器 用的APB1 APB2 及 定时器频率
上午想要用Timer10做相对精确的延时功能,但是用示波器发现实际延时数值总是只有一半,百思不得其解.仔细查阅各处资料结合实际研究后对stm32f407的14个定时器的时钟做一个总结: 下面来源: h ...
- SOP、DIP、PLCC、TQFP、PQFP、TSOP、BGA封装解释
1. SOP封装SOP是英文Small Outline Package的缩写,即小外形封装.SOP封装技术由1968-1969年菲利浦公司开发成功,以后逐渐派生出SOJ(J型引脚小外形封装).TSOP ...
- Socket连接与HTTP连接
我们在传输数据时,可以只使用(传输层)TCP/IP协议,但是那样的话,如果没有应用层,便无法识别数据内容,如果想要使传输的数据有意义,则必须使用到应用层协议,应用层协议有很多,比如HTTP.FTP.T ...
- UI进阶 动画
前言:所谓动画,即应用界面上展示的各种过渡效果,不过其实没有动画并不影响我们产品的功能实现 一.动画 1.动画可以达到的效果 传达状态 提高用户对直接操作的感知 帮助用户可视化操作的结果 2.使用动画 ...
- 框架学习笔记:深度解析StrangeIoC内部运行机制
StrangeIoC的设计和RobotLegs一致,所以我的解析会对照RobotLegs来看. 整个框架使用的是MVCS的模式,关于MVCS模式大家可以点这里进行查看,这里就不谈了,既然Strange ...
- Boost的Serialization和SmartPoint搭配使用
准确来说,这篇博文并不是译文,而是一篇某个网页中代码改写而来.原文章中的代码存在几处严重错误,网页又不提供留言功能(不是没有而是一个没有留言功能的留言板).4年过去了,作者对这些错误不更正让人无法接受 ...
- heidsoft logo