题目描述 Description

最经,skyzhong得到了一本好厉害的字典,这个字典里整整有n个单词(1<=n<=200000)

现在skyzhong需要在字典里查询以某一段字母开头的单词

如:skyzhong想查询a

那么只要是a开头的单词就可以了

skyzhong只想知道里面有没有这一个单词(因为没有他就不查了)

若有,请输出YES。若没有,请输出NO

输入描述 Input Description

第一行一个数n

第二行到第n+1行,一行一个字符串

再下一行一个数m,表示skyzhong想要查询的次数

接着m行,一行一个字符串,表示skyzhong想要查的东西

输出描述 Output Description

共m行,若有这字串输出YES,否则输出NO

样例输入 Sample Input

3

asd

asfdghj

asfd

3

asd

asdghj

asf

样例输出 Sample Output

YES

NO

YES

数据范围及提示 Data Size & Hint

字符串只有小写字母,且长度≤8

 Trie树模板题
 1.指针版
  1. #include<bits/stdc++.h>
  2.  
  3. #define N 2000010
  4.  
  5. using namespace std;
  6. //map<string ,int>W; //map淼
  7. int n,m,len,tot,trie[N][],sum[N],v[N];
  8. char s[];
  9. struct node{
  10. int count;
  11. node *next[];
  12. node(){count=,memset(next,,sizeof(next));}
  13. }*rt;
  14.  
  15. void insert(){
  16. node *r=rt;
  17. char *word=s;
  18. while(*word){
  19. int id=*word-'a';
  20. if(r->next[id]==NULL) r->next[id]=new node;//r->next[id]=build();
  21. r=r->next[id];
  22. r->count++;
  23. word++;
  24. }
  25. }
  26. int search(){
  27. node *r=rt;
  28. char *word=s;
  29. while(*word){
  30. int id=*word-'a';
  31. r=r->next[id];
  32. if(r==NULL) return ;
  33. word++;
  34. }return r->count;
  35. }
  36.  
  37. int main()
  38. {
  39. rt=new node;
  40. cin>>n;
  41. for(int i=;i<=n;i++){
  42. cin>>s;
  43. insert();
  44. }cin>>m;
  45. for(int i=;i<=m;i++){
  46. cin>>s;
  47. if(search()!=){
  48. cout<<"YES\n";
  49. }else cout<<"NO\n";
  50. }return ;
  51. }

  

  2.数组模拟

  1. #include<bits/stdc++.h>
  2.  
  3. #define N 2000010
  4.  
  5. using namespace std;
  6. //map<string ,int>W; //map淼
  7. int n,m,len,tot,trie[N][],rt,sum[N],v[N];
  8. char s[];
  9. void insert(){
  10. len=strlen(s);
  11. rt=;
  12. for(int i=;s[i];i++){
  13. int id=s[i]-'a';
  14. if(!trie[rt][id]) trie[rt][id]=++tot;
  15. sum[trie[rt][id]]++;
  16. rt=trie[rt][id];
  17. }
  18. }
  19. int search(){
  20. len=strlen(s);
  21. rt=;
  22. for(int i=;s[i];i++){
  23. int id=s[i]-'a';
  24. if(!trie[rt][id]) return ;
  25. rt=trie[rt][id];
  26. }return sum[rt];
  27. }
  28. int main()
  29. {
  30. cin>>n;
  31. for(int i=;i<=n;i++){
  32. cin>>s;
  33. insert();
  34. }cin>>m;
  35. for(int i=;i<=m;i++){
  36. cin>>s;
  37. if(search()!=){
  38. cout<<"YES\n";
  39. }else cout<<"NO\n";
  40. }return ;
  41. }

  3.Map水题版

  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4. map<string ,int>W;
  5. int n,m,len;
  6. char s[];
  7. int main()
  8. {
  9. cin>>n;
  10. for(int i=;i<=n;i++){
  11. cin>>s;
  12. len=strlen(s);
  13. for(int i=len;i>=;i--){
  14. s[i]='\0';
  15. W[s]++;
  16. }
  17. }cin>>m;
  18. for(int i=;i<=m;i++){
  19. cin>>s;
  20. if(W[s]!=){
  21. cout<<"YES\n";
  22. }else cout<<"NO\n";
  23. }return ;
  24. }

  

Trie树初探,参考博客:TRTTG

#1014 : Trie树

建树+查找

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #define N 10101010
  5.  
  6. using namespace std;
  7.  
  8. int n,m,tot,tri[N][],vi[N];
  9. string s;
  10.  
  11. void insert(){
  12. int l=s.length(),root=;
  13. for(int i=;i<l;i++){
  14. int id=s[i]-'a';
  15. if(!tri[root][id]) tri[root][id]=++tot;
  16. root=tri[root][id];
  17. vi[root]++;
  18. }
  19. }
  20.  
  21. int ask_(){
  22. int l=s.length(),root=;
  23. for(int i=;i<l;i++){
  24. int id=s[i]-'a';
  25. if(!tri[root][id]) return ;
  26. root=tri[root][id];
  27. }
  28. return vi[root];
  29. }
  30.  
  31. int main()
  32. {
  33. scanf("%d",&n);
  34. for(int i=;i<=n;i++) {
  35. cin>>s;
  36. insert();
  37. }
  38. scanf("%d",&m);
  39. for(int i=;i<=m;i++){
  40. cin>>s;
  41. printf("%d\n",ask_());
  42. }
  43. return ;
  44. }
 

codevs——4189 字典&&HihoCoder #1014 : Trie树的更多相关文章

  1. Hihocoder #1014 : Trie树 (字典数树统计前缀的出现次数 *【模板】 基于指针结构体实现 )

    #1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助, ...

  2. hihoCoder #1014 : Trie树 [ Trie ]

    传送门 #1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互 ...

  3. hihoCoder 1014 Trie树 (Trie)

    #1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 小Hi和小Ho是一对好朋友.出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮 ...

  4. hihoCoder 1014 : Trie树(字典树)

    传送门 Description 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这一天,他们遇到了一本词典,于是小Hi就向小 ...

  5. hihoCoder#1014 Trie树 (前缀树)

    题目大意:给一本有n个单词的词典,有m次询问,每次询问的是该词典中有多少个单词有共同的某个前缀. 题目分析:在添加单词建立trie的时候,每经过一个节点就意味着该节点和它的各级祖先节点是某个单词的前缀 ...

  6. hihocoder 1014: Trie树(Trie树模板题)

    题目链接 #include<bits/stdc++.h> using namespace std; ; struct T { int num; T* next[]; T() { num=; ...

  7. hiho #1014 : Trie树

    #1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助, ...

  8. Codevs 4189 字典(字典树Trie)

    4189 字典 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 传送门 题目描述 Description 最经,skyzhong得到了一本好厉害的字典,这个字典里 ...

  9. #1014 : Trie树 HihoCoder(字典树)

    描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这一天,他们遇到了一本词典,于是小Hi就向小Ho提出了那个经典的问题: ...

随机推荐

  1. QSettings读写注冊表、配置文件

    简述 普通情况下.我们在开发软件过程中,都会缓存一些信息到本地,能够使用轻量级数据库sqlite.也能够操作注冊表.读写配置文件. 关于QSettings的使用前面已经介绍过了.比較具体,见" ...

  2. 虚拟化(二):虚拟化及vmware workstation产品使用

    虚拟化(一):虚拟化及vmware产品介绍 vmware workstation的最新版本号是10.0.2. 相信大家也都使用过,当中的简单的虚拟机的创建.删除等,都非常easy.这里就不再具体说明了 ...

  3. LeetCode 463. Island Perimeter (岛的周长)

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  4. linux下非root用户怎样改动root权限的文件

           在linux下会出现把一些配置文件參数配错.rootpassword忘记等导致系统无法启动或进入root的窘迫境界.本文以redhat  enterprise linux server ...

  5. 软件project—思考项目开发那些事(一)

    阅读文件夹: 1.背景 2.项目管理,质量.度量.进度 3.软件开发是一种设计活动而不是建筑活动 4.高速开发(简单的系统结构与复杂的业务模型) 5.技术人员的业务理解与产品经理的业务理解的终于业务模 ...

  6. 1250 Fibonacci数列

    1250 Fibonacci数列  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description 定义:f ...

  7. tiny4412 裸机程序 九、串口排查驱动原因及字符图片显示【转】

    本文转载自:http://blog.csdn.net/eshing/article/details/37410571 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[+]   一 ...

  8. ssh连接超时问题解决方案,每一种方案都可以

    1.服务端修改 vim /etc/ssh/sshd_config 修改 ClientAliveInterval 60 ClientAliveCountMax 40 60秒,向客户端发送一次请求. 超过 ...

  9. canvas做的一个写字板

    <!DOCTYPE html><html><head><title>画板实验</title> <meta charset=" ...

  10. 设计模式 |备忘录模式(memento)

    定义: 在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可以将该对象恢复到原先保存的状态. 结构:(书中图,侵删) Originator:需要备份的类(写在便签上 ...