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个字符串 对于每一个询问字符串 你需要在 ...
随机推荐
- Confluence 6 管理员联系表单的后台配置界面
管理员联系表单的后台配置界面截图和配置. 对输入的数据进行编辑和选择是否启用发送电子邮件给管理员 https://www.cwiki.us/display/CONFLUENCEWIKI/Configu ...
- java Properties (属性集)
加载Properties Properties downloadLog = new Properties(); try { //加载logFile文件 downloadLog.load(new Fil ...
- codeforce 240E
/* 最小树形图+保存路径 第一次想错了,各种wa,tle后网上看资料,找到一篇错误的题解... 最后用对着正解分析了一波,感觉对最小树形图又有了新的理解:最小树形图的精髓在于每张图更新的时间信息! ...
- bzoj1195 神奇的ac自动机+状态压缩dp
/* 难的不是ac自动机,是状态压缩dp 之前做了一两题类似题目,感觉理解的还不够透彻 */ #include<iostream> #include<cstdio> #incl ...
- java提取出一个字符串里面的Double类型数字
String str="hh\n1\n22\n798.809\n0.89\n"; String regex="\\d+(?:\\.\\d+)?" ...
- 正则表达式过滤html标签
1.说明:需要使用非贪婪模式 2.示例 过滤所有span标签: var newContent = Regex.Replace(htmlContent, "<span.*?>.*? ...
- Ubuntu下安装kate编辑器
Ubuntu下安装kate编辑器 Ubuntu 下安装kate编辑器 #sudo apt-get install kate 安装kconsole #sudo apt-get install kco ...
- Tensorflow生成唐诗和歌词(下)
整个工程使用的是Windows版pyCharm和tensorflow. 源码地址:https://github.com/Irvinglove/tensorflow_poems/tree/master ...
- 435. Non-overlapping Intervals
Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...
- 494. Target Sum
You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symb ...