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 题意:给你许许多多的木棍,没条木棍两端有两种颜色,问你在将木棍相连时,接触的端点颜色 ...
随机推荐
- bzoj 2190 仪仗队(欧拉函数)
2190: [SDOI2008]仪仗队 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 2245 Solved: 1413[Submit][Statu ...
- 当心回车符破坏你的JSON数据
今天发现系统中一个地方获取JSON数据时,时而失败,时而成功,最后发现是回车符搞的鬼. 当你的JSON中有回车符时,会致使你的JSON出现格式错误:解决办法是在保存数据,或整理数据向客户端输出时将回车 ...
- [ASP.NET MVC] Child actions are not allowed to perform redirect
我在Umbraco平台下,用MVC(SurfaceController)开发时,遇到这个问题 MemberEdit是一个partial View [HttpGet] [ActionName(" ...
- oracle 11g 之 result cache
oracle 11g 之 result cache 今天是2013-10-12,打算最近时间研究一下shared pool的相关原理以及awr报告分析.今天学习一下在oracle 11g shared ...
- do{...}while(0)的作用
不是为了循环的while. 1.用于宏定义,保证宏一定按照想要的方式执行. #define foo(x) start(x); end(x) if(flag) foo(x); 扩展以后的结果 ...
- jquery 绑定省份和城市
前台代码: <asp:DropDownList runat="server" ID="ddlProvince"></asp:DropDownL ...
- SQL SERVER 2008/2012/2012R2/2014 设置开启远程连接(sa配置)
本文方案适用于Microsoft Sql Server 2008/2012/2012 r2/2014版本,以下简称MSSQLSERVER. MSSQL默认是不允许远程连接,并且禁用sa账户的.如果想要 ...
- ALV的报表对用户定义格式的控制(ALV I_SAVE)
很多ALV的报表都需要手动的进行设置格式以使数据看上去更有意义和条理,如果每次进来都重新操作一遍是很烦人的,所以SAP有提供了一个保存格式的功能,保存格式可以是 '缺省设置' 和 '特定用户' 两种 ...
- pjsip视频通信开发(上层应用)之拨号界面整体界面功能实现
在前面的几章里面写了显示.键盘.拨号.删除功能,这里我将他们进行组合,形成一个拨号键盘全部功能.首先是布局 <LinearLayout xmlns:android="http://sc ...
- POJ1463:Strategic game(树形DP)
Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot f ...