【题目链接】:http://codeforces.com/contest/514/problem/C

【题意】



给你n个字符串;

然后给你m个询问;->m个字符串

对于每一个询问字符串

你需要在n个字符串里面找到和它的长度相同,且只有一个位置的字符不同的字符串;

或者告知这是不存在的;

【题解】



写个字典树;

在每一位多了两种选择;

(即更改这个字符)

然后前面已经确定匹配的不要重新匹配;->不然会超时

有改和不改两种可能.

注意最后必须要改一个字符.

字典树以后还是用0作为根节点吧。不然tot老是忘记赋初值1;



【Number Of WA】



4



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)
#define ms(x,y) memset(x,y,sizeof x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e6+1000; struct node
{
int c[3],flag;
}; int n,m,tot=1,len;
node tree[N];
string s;
//fi(1,0,0)
bool fi(int temp,int pos,int gai)
{
if (pos>=len)
return gai&&tree[temp].flag;
if (!gai)
for (char a='a';a<='c';a++)
if (a!=s[pos])
{
if (tree[temp].c[a-'a'] && fi(tree[temp].c[a-'a'],pos+1,1))
return true;
}
if (tree[temp].c[s[pos]-'a'])
return fi(tree[temp].c[s[pos]-'a'],pos+1,gai);
return false;
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n),rei(m);
rep1(i,1,n)
{
cin >> s;
len = s.size();
int temp = 1;
rep1(j,0,len-1)
{
if (!tree[temp].c[s[j]-'a'])
tree[temp].c[s[j]-'a']=++tot;
temp = tree[temp].c[s[j]-'a'];
}
tree[temp].flag = 1;
}
rep1(i,1,m)
{
cin >> s;
len = s.size();
if (fi(1,0,0))
puts("YES");
else
puts("NO");
}
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}

【codeforces 514C】Watto and Mechanism(字典树做法)的更多相关文章

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

    题目链接  Watto and Mechanism 题意  给出$n$个串(相当于字典),然后给出$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. XPath Nodes

    教程 https://www.w3schools.com/xml/xpath_nodes.asp 节点之间的关系 Parent,Children,Siblings,Ancestors,Descenda ...

  2. Codeforces--631A--Interview(位运算)

     Interview Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:262144KB     ...

  3. Network(Tarjan+LCA)

    http://poj.org/problem?id=3417 元宵节+情人节晚上刷的题,纪念一下.. 题意:给出n个点,m条边,然后Q个询问,每次询问输入一条边,输出加入此边后桥的个数.. #incl ...

  4. [App Store Connect帮助]二、 添加、编辑和删除用户(6)生成 API 密钥

    如果已批准您访问 App Store Connect API,您可以生成 API 密钥,以便使用该密钥配置.认证和使用 App Store Connect 服务. 有关管理和保护您密钥的更多信息,请参 ...

  5. [Swift通天遁地]二、表格表单-(9)快速创建一个美观强大的表单

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  6. 城市平乱 ---- Dijkstra

    题解 : 以暴乱城市 为 源点 向所有点做最短路径 , 然后检查每个不对到暴乱城市的 最短距离 #include<stdio.h> #include<string.h> #in ...

  7. Java常用集合类

    上述类图中,实线边框的是实现类,比如ArrayList,LinkedList,HashMap等,折线边框的是抽象类,比如AbstractCollection,AbstractList,Abstract ...

  8. Android内存管理(9)*MAT:Heap Dump,Shallow Heap,Retained Heap,Dominating Tree,GC Roots等的含义

    原文: http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.mat.ui.help%2Fconcepts%2Fheapdump.ht ...

  9. js基础---object对象

    //**********************************复杂JSON举例**************************************** var Jsondata={d ...

  10. HTML 表单 存为EXCEL文件时 中文显示乱码

    在做宣传品发放系统时,需求要把数据库查询的记录生成表单并转存excel文件. 在转存的EXCEL文件中文显示乱码,表格和其他字符正常,检查后发现是创建EXCEL文件打开模式不对 之前: myfile ...