题目链接  Watto and Mechanism

题意  给出$n$个串(相当于字典),然后给出$m$个询问。

每个询问以字符串的形式给出,你需要改变这个字符串中的任意一个字符

(必须改变且只能改变一个)

如果改变之后可以成为$n$个串中的一个字符串,则输出$YES$, 否则输出$NO$。

字母集合为$\left\{ a, b, c \right\}$

考虑字典树。

首先把$n$个单词插入字典树中。

询问的时候用$dfs$,$flag$表示搜索到当前是否已经改变过一个字符。

如果已经改变过那只能按照当前剩下的字符串一条路查询下去。

否则可以按老字符或新字符进行查询。

(这题很卡内存)

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) const int N = 2e7 + 3; char s[600010];
int n, q, ans, len, tot = 0;
int ch[N][3];
bitset <N> cnt; int newnode(){
++tot;
rep(i, 0, 2) ch[tot][i] = 0;
cnt[tot] = 0;
return tot;
} void insert(char* s){
int now = 0;
for (int i = 0; s[i]; ++i){
int w = s[i] - 'a';
if (!ch[now][w]) ch[now][w] = newnode();
now = ch[now][w];
}
cnt[now] = 1;
} void dfs(int x, int flag, int now){
if (ans) return;
if (x == len && flag && cnt[now]){ ans = 1; return;}
if (x >= len) return;
if (flag){
int w = s[x] - 'a';
if (ch[now][w]) dfs(x + 1, flag, ch[now][w]);
} else{
rep(i, 0, 2)
if (i != s[x] - 'a' && x + 1 == len && ch[now][i] && cnt[ch[now][i]]) ans = 1; rep(i, 0, 2) if (i != s[x] - 'a' && ch[now][i]){
dfs(x + 1, 1, ch[now][i]);
} int w = s[x] - 'a';
if (ch[now][w]){
dfs(x + 1, flag, ch[now][w]);
} }
} int main(){ scanf("%d%d", &n, &q);
rep(i, 1, n){
scanf("%s", s);
insert(s);
} rep(i, 1, q){
scanf("%s", s);
len = strlen(s);
ans = 0;
dfs(0, 0, 0);
puts(ans ? "YES" : "NO");
} return 0;
}

Codeforces 514C Watto and Mechanism(字典树)的更多相关文章

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

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

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

  3. CF Watto and Mechanism (字典树+深搜)

    Watto and Mechanism time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  4. Codeforces 514C Watto and Mechanism 【Trie树】+【DFS】

    <题目链接> 题目大意:输入n个单词构成单词库,然后进行m次查询,每次查询输入一个单词(注意这些单词只由a,b,c构成),问该单词库中是否存在与当前查询的单词有且仅有一个字符不同的单词. ...

  5. codeforces gym #101161F-Dictionary Game(字典树+树上删边游戏)

    题目链接: http://codeforces.com/gym/101161/attachments 题意: 给一个可以变化的字典树 在字典树上删边 如果某条边和根节点不连通那么这条边也删除 谁没得删 ...

  6. CodeForces 706D Vasiliy's Multiset (字典树查询+贪心)

    题意:最开始的时候有一个集合,集合里面只有一个元素0,现在有q次操作,操作分为3种: + x: 表示向集合中添加一个元素x - x:表示删除集合中值为x的一个元素 ? x:表示查询集合中与x异或的最大 ...

  7. Codeforces 577E Ann and Half-Palindrome 字典树

    题目链接 题意: 若一个字符串是半回文串.则满足第一位和最后一位相等, 第三位和倒数第三位相等.如此类推. 给定一个字符串s,输出s的全部子串中的半回文串字典序第k大的 字符串. good[i][j] ...

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

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

  9. codeforces 706D (字典树)

    题目链接:http://codeforces.com/problemset/problem/706/D 题意:q次操作,可以向多重集中增添,删除,询问异或最大值. 思路:转化为二进制用字典树存储,数字 ...

随机推荐

  1. bootstrap 翻页(对齐的链接)

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  2. Shell脚本调用Oralce数据库SQL文生产日志

    #!/bin/shexport LANG="zh.CN.GBK" echo -n "******************************************* ...

  3. iptables 过滤字符串

    iptables 过滤字符串 1. 开启iptables iptables -P OUTPUT ACCEPT       ###允许输出链 service iptables save          ...

  4. UIScreen, UIWindow

    模仿书上或网上的例子,每次最开始就是 在 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: ...

  5. visibilitychange 标签可见性

    var pageVisibility = document.visibilityState;// 监听 visibility change 事件document.addEventListener('v ...

  6. 用xtrabackup实现mysql的主从复制 阿里云rds到自己创建mysql

    来源 http://blog.51cto.com/825536458/1803968参考https://segmentfault.com/a/1190000003063874 如果我们用传统的mysq ...

  7. input动态模糊查询的实现方式

    最近在用jQuery实现动态模糊查询的时候,找了挺久都没有找到像Vue.js的watch属性这么好用的动态模糊查询方法.就分享一下目前遇到的坑和可以实现动态查询的几种方式. 1.jQuery的chan ...

  8. python--进程内容补充

    一. 进程的其他方法 进程id, 进程名字, 查看进程是否活着(is_alive()), terminate()发送结束进程的信号 import time import os from multipr ...

  9. tensorboard以时间命名每一个文件夹

    tensorboard 有一个良好的命名习惯以时间命名每一个文件夹,例如**20190523_081232** ''' from datetiome import datetime dir = os. ...

  10. cs229_part5

    这部分主要补充一些cs229没涉及到,但是实际上非常重要,而且是实际中真正会用的一些算法,即集成学习. 集成学习 问题背景 既然我们已经知道了很多学习算法,这些算法最终会输出一个结果.能不能把这些结果 ...