Watto and Mechanism
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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

Output

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

Sample test(s)
input
2 3
aaaaa
acacaca
aabaa
ccacacc
caaac
output
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 (字典树+深搜)的更多相关文章

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

  2. 【codeforces 514C】Watto and Mechanism(字典树做法)

    [题目链接]:http://codeforces.com/contest/514/problem/C [题意] 给你n个字符串; 然后给你m个询问;->m个字符串 对于每一个询问字符串 你需要在 ...

  3. HDU-5423 Rikka with Tree。树深搜

    Rikka with Tree 题意:给出树的定义,给出树相似的定义和不同的定义,然后给出一棵树,求是否存在一颗树即和其相似又与其不同.存在输出NO,不存在输出YES. 思路:以1号节点为根节点,我们 ...

  4. Codeforces 514C Watto and Mechanism(字典树)

    题目链接  Watto and Mechanism 题意  给出$n$个串(相当于字典),然后给出$m$个询问. 每个询问以字符串的形式给出,你需要改变这个字符串中的任意一个字符 (必须改变且只能改变 ...

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

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

  6. 【离线】【深搜】【树】Codeforces 707D Persistent Bookcase

    题目链接: http://codeforces.com/problemset/problem/707/D 题目大意: 一个N*M的书架,支持4种操作 1.把(x,y)变为有书. 2.把(x,y)变为没 ...

  7. CodeM美团点评编程大赛初赛B轮 黑白树【DFS深搜+暴力】

    [编程题] 黑白树 时间限制:1秒 空间限制:32768K 一棵n个点的有根树,1号点为根,相邻的两个节点之间的距离为1.树上每个节点i对应一个值k[i].每个点都有一个颜色,初始的时候所有点都是白色 ...

  8. 字典树+博弈 CF 455B A Lot of Games(接龙游戏)

    题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...

  9. nyoj 230/poj 2513 彩色棒 并查集+字典树+欧拉回路

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=230 题意:给你许许多多的木棍,没条木棍两端有两种颜色,问你在将木棍相连时,接触的端点颜色 ...

随机推荐

  1. 学习使用Markdown标记语言

    学习如何使用Markdown进行文本编辑 使用教程   大家若是经常逛Github,就知道其中有一个文件叫做README.MD.我一开始也不知道这个.MD是什么意思,后来我自己写了一次,就知道了这一种 ...

  2. Hibernate关联关系之——单向n-1

    1 .单向 n-1 关联只需从n的一端可以访问1的一端 2.域模型: 从Order到Customer的多对一单向关联需要在Order类中定义一个Customer属性,而在Customer类中无需定义存 ...

  3. HDU 5787 K-wolf Number (数位DP)

    K-wolf Number 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5787 Description Alice thinks an integ ...

  4. My97DatePicker的calendar.js的反混淆

    eval(string)函数 <script> eval(function(p, a, c, k, e, d) { p = 'function p(){console.log(" ...

  5. POJ 3312 Mahershalalhashbaz, Nebuchadnezzar, and Billy Bob Benjamin Go to the Regionals (水题,贪心)

    题意:给定 n 个字符串,一个k,让你把它们分成每组k个,要保证每组中每个字符串长度与它们之和相差不能超2. 析:贪心策略就是长度相差最小的放上块. 代码如下: #pragma comment(lin ...

  6. POJ 1573 (13.10.11)

    Description A robot has been programmed to follow the instructions in its path. Instructions for the ...

  7. CSS实现子级窗口高度随低级窗口高度变化

    纯粹使用使用height:100%;或者height:auto;来定义内部容器自适应高度,都无法实现让内部容器高度随着外部父容器高度变化而变化,所以我们必需要使用position绝对定位属性来配合协助 ...

  8. android EditText控制光标的位置

    利用自定义键盘,需要手动删除编辑框中的文本时,会根据光标的位置来删除字符.那么,如何来控制光标呢,android为我们提供了哪些方法,来处理光标呢? 这里提供几个自己写的方法,根据这些方法可以满足在光 ...

  9. setbuf和freopen

    看memcached代码的时候学习了一个api,setbuf,可以设置文件流的缓冲区.        #include <stdio.h> void setbuf(FILE *stream ...

  10. CSS 边框的宽度

    边框的宽度 您可以通过 border-width 属性为边框指定宽度. 为边框指定宽度有两种方法:可以指定长度值,比如 2px 或 0.1em:或者使用 3 个关键字之一,它们分别是 thin .me ...