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 题意:给你许许多多的木棍,没条木棍两端有两种颜色,问你在将木棍相连时,接触的端点颜色 ...
随机推荐
- Spring Named Parameters examples in SimpleJdbcTemplate
In JdbcTemplate, SQL parameters are represented by a special placeholder "?" symbol and bi ...
- javascript中document对象的属性和方法
document.documentElement; document.firstChild;document.childNodes[0];// 取得对<html>的引用document.b ...
- ASP.Net自定义重写Http Server标头
Net中我们为了安全或其他原因起见 可能需要修改我们的标头报文等 以下方法我们通过使用HTTP Module来使用编程的方式来去除或修改它 首先我们自定义一个类CustomServerHeaderMo ...
- Sql CLR
using System;using System.Data;using System.Data.SqlClient;using System.Data.SqlTypes;using Microsof ...
- getopt使用例子
getopt是linux下获取程序启动参数的函数 #include <unistd.h> int getopt(int argc, char * const argv[], ...
- 异常捕捉 ( try catch finally ) 你真的掌握了吗?
前言:java 中的异常处理机制你真的理解了吗?掌握了吗?catch 体里遇到 return 是怎么处理? finally 体遇到 return 怎么办?finally 体里有 System.exit ...
- 三分钟掌握 JUnit3.0
曾经公司做过一个.net的项目,在项目开发的过程中.我们採用的是分层的开发方式,大家先在一起讨论接口, 然后讨论完以后,形成文档,然后依照文档进行开发!这样就有一个问题,你必需要保证你的接口是正确的. ...
- cardsui-for-android
https://github.com/Androguide/cardsui-for-android cardsui-for-android-master.zip
- CircleDisplay
https://github.com/PhilJay/CircleDisplay
- Scrapy 对不同的Item进行分开存储
在Piperlines里面进行对象的判断, def process_item(self, item, spider): if item.__class__ == BaseItem : #savexxx ...