Codeforces 514C Watto and Mechanism 【Trie树】+【DFS】
<题目链接>
题目大意:
输入n个单词构成单词库,然后进行m次查询,每次查询输入一个单词(注意这些单词只由a,b,c构成),问该单词库中是否存在与当前查询的单词有且仅有一个字符不同的单词。
解题分析:
本题将单词库中所有的单词先建trie树,然后进行容错数为1的字符串匹配,主要是在trie树上跑DFS,具体步骤见代码:
#include <bits/stdc++.h>
using namespace std; const int N = 6e5+;
int n,m,pos,nxt[N*][],num[N*];
char s[N]; void Insert(char *s){
int now=;
for(int i=;s[i];i++){
int to=s[i]-'a';
if(!nxt[now][to])nxt[now][to]=++pos;
now=nxt[now][to];
}
num[now]=;
}
bool query(int now,int lc,int cur){
if(s[lc]=='\0'){ //搜到字符串末尾
if(cur== && num[now]==)return true; //恰好有一处不同
else return false;
}
int to=s[lc]-'a';
if(nxt[now][to]){ //自然匹配
if(query(nxt[now][to],lc+,cur))return true;
}
if(cur==){ //同时搜一下如果恰好不同的地方是这一位的情况
for(int i=;i<;i++){
int to=i;
if(to==s[lc]-'a' || !nxt[now][to])continue; //跳过与当前位相同和不存在的情况
if(query(nxt[now][to],lc+,))return true;
}
}
return false;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%s",s);
Insert(s);
}
while(m--){
scanf("%s",s);
if(query(,,))puts("YES");
else puts("NO");
}
}
Codeforces 514C Watto and Mechanism 【Trie树】+【DFS】的更多相关文章
- Codeforces 514C Watto and Mechanism(字典树)
题目链接 Watto and Mechanism 题意 给出$n$个串(相当于字典),然后给出$m$个询问. 每个询问以字符串的形式给出,你需要改变这个字符串中的任意一个字符 (必须改变且只能改变 ...
- Codeforces 633C Spy Syndrome 2 | Trie树裸题
Codeforces 633C Spy Syndrome 2 | Trie树裸题 一个由许多空格隔开的单词组成的字符串,进行了以下操作:把所有字符变成小写,把每个单词颠倒过来,然后去掉单词间的空格.已 ...
- Trie树 + DFS - CSU 1457 Boggle
Boggle Problem's Link: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1457 Mean: 给定n个串,有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 852G Bathroom terminal 【Trie树】
<题目链接> 题目大意: 现在给定出n个字符串,并且进行m此询问,每次询问给出一个匹配串,每次询问都给出该匹配串能够匹配的字符串个数(题目只出现字符'a'~'e').'?'可以看成任意字符 ...
- 2018.10.20 NOIP模拟 巧克力(trie树+dfs序+树状数组)
传送门 好题啊. 考虑前面的32分,直接维护后缀trietrietrie树就行了. 如果#号不在字符串首? 只需要维护第一个#前面的字符串和最后一个#后面的字符串. 分开用两棵trie树并且维护第一棵 ...
- codeforces 665E E. Beautiful Subarrays(trie树)
题目链接: E. Beautiful Subarrays time limit per test 3 seconds memory limit per test 512 megabytes input ...
- 【codeforces 514C】Watto and Mechanism(字典树做法)
[题目链接]:http://codeforces.com/contest/514/problem/C [题意] 给你n个字符串; 然后给你m个询问;->m个字符串 对于每一个询问字符串 你需要在 ...
随机推荐
- 关闭VirtualBox虚拟机的时钟同步
原文链接:关闭VirtualBox虚拟机的时钟同步 在VirtualBox的虚拟机上默认虚拟机的时间是会和物理机同步的,但可以通过下面的命令来关闭 1. 首先查看虚拟机列表 VBoxManage li ...
- day04 运算符 流程控制 (if while/of)
1. 运算符算数运算符 + - * / int / float :数字类型 # print(10 + 3.1)# print(10 / 3)# print(10 // 3)# print(10 % 3 ...
- 基于ajax实现的登录
一.需要知道的新知识点 1.刷新验证码.给src属性加一个?号.加个?会重新去请求 //#给验证码刷新 $(".vialdCode_img").click(function () ...
- WPA2-PSK无线密码破解
无线网络WIFI(wireless Fidelity )正确发音 /wai fai/ 是一个建立在IEEE 802.11标准的无线局域网,目前主流的无线上网模式主要有两种分别是 GRPS(手机无线上网 ...
- Loadrunner11.0 录制手机App脚本的方法二
lr11安装一个移动补丁以后,就可以支持抓包文件直接转换为http/html协议的脚本.下面简单说一下过程. 1.工作思路:手机连接可上外网wifi,在手机wifi中设置本机ip的网络地址,然后在本机 ...
- 步步为营103-ZTree 二级联动
1:添加引用 <%--流程类别多选--引用js和css文件--开始--%> <link rel="stylesheet" href="../css/zT ...
- IDEA的字体设置
最后点击ok
- Tensorflow:DCGAN生成手写数字
参考地址:https://blog.csdn.net/miracle_ma/article/details/78305991 使用DCGAN(deep convolutional GAN):深度卷积G ...
- lsattr chattr 文件安全设置
[root@test_android_client_download xianlaigames]# lsattr -------------e- ./jilinmj.apk[root@test_and ...
- cmake方式使用vlfeat
目录 environment statement compile vlfeat with cmake compile example project with cmake 1. make sure c ...