Trie(Codeforces Round #291 (Div. 2))
来源:互联网 发布:java条形码生成代码 编辑:程序博客网 时间:2024/05/19 01:13
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 withn strings. Then the mechanism should be able to process queries of the following type: "Given strings, determine if the memory of the mechanism contains stringt that consists of the same number of characters ass 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 ofn initial lines and m queries. He decided to entrust this job to you.
The first line contains two non-negative numbers n andm (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 consistsonly 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 3aaaaaacacacaaabaaccacacccaaac
YESNONO
思路:构建trie树后,每次判断,对每个位置枚举每种字母替换(因为只有三种字母),然后进行判断
#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=1000010;const int SIGMA_SIZE=3;int N,M;char str[maxn];struct Trie{ int ch[maxn][4]; int val[maxn]; int sz; void clear(){memset(ch[0],0,sizeof(ch[0]));sz=1;} int idx(char x){return x-'a';} void insert(char *s,int v) { int u=0; int n=strlen(s); for(int i=0;i<n;i++) { int c=idx(s[i]); if(!ch[u][c]) { memset(ch[sz],0,sizeof(ch[sz])); val[sz]=0; ch[u][c]=sz++; } u=ch[u][c]; } val[u]=v; } bool solve(char *s) { int u=0; int n=strlen(s); for(int i=0;i<n;i++) { int c=idx(s[i]); for(int j=0;j<SIGMA_SIZE;j++) { if(c==j||ch[u][j]==0)continue; int u2=ch[u][j]; bool flag=true; for(int k=i+1;k<n;k++) { int c2=idx(s[k]); if(!ch[u2][c2]) { flag=false; break; } u2=ch[u2][c2]; } if(flag&&val[u2])return true; } if(!ch[u][c])return false; u=ch[u][c]; } return false; }}tree;int main(){ while(scanf("%d%d",&N,&M)!=EOF) { tree.clear(); for(int i=1;i<=N;i++) { scanf("%s",str); tree.insert(str,1); } while(M--) { scanf("%s",str); if(tree.solve(str))printf("YES\n"); else printf("NO\n"); } } return 0;}
- Trie(Codeforces Round #291 (Div. 2))
- Codeforces Round #367 (Div. 2) dp,trie
- Codeforces Round #311 (Div. 2)判环+Trie 好
- Codeforces Round #367 (Div. 2) Vasiliy's Multiset xor trie
- Codeforces Round #367 (Div. 2) D Trie树
- Trie树 Codeforces Round #291 (Div. 2) C. Watto and Mechanism
- Codeforces Round #291 (Div. 2) C. Watto and Mechanism Trie字典树+dfs
- Codeforces Round #291 (Div. 2)-C. Watto and Mechanism(Trie树)
- Codeforces Round #371 (Div. 2) C. Sonya and Queries (01trie树)
- 【 Codeforces Round #367 (Div. 2) D】 Vasiliy's Multiset (Trie 按数位建字典树)
- Codeforces Round #367 (Div. 2) D Vasiliy's Multiset(01Trie)
- Codeforces Round #291 (Div. 2)(C)
- Codeforces Round #291 (Div. 2)(D)
- Codeforces Round #291 (Div. 2)
- Codeforces Round #291 (Div. 2)
- Codeforces Round #291 (Div. 2)
- Codeforces Round #291 (Div. 2)
- Codeforces Round #291 (Div. 2)
- UIWebView---JavaScriptCore和JS交互
- Apache + tomcat实现高并发负载均衡方案(三)----实现session复制的集群
- js动态引入JS文件
- PUTTY配置导出
- flashback query
- Trie(Codeforces Round #291 (Div. 2))
- 微信分享的完善,以及剪贴板的应用
- 新ip地址
- PHP之AJAX
- 在Centos7下,在线进行lv的扩展
- JSP学习笔记(五):自定义标签-JSP1.x
- java EE 架构师 需要具备的知识
- ECharts,一个javascript 互动图表库,使用例子。
- JSP学习笔记(六):自定义标签-JSP2.x