码迷,mamicode.com
首页 > 其他好文 > 详细

字典树模板

时间:2018-10-02 22:30:30      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:space   ++   span   code   查询   open   names   ios   for   

  • 查找该字符串是不是已经出现过
 1 //在给出的字符串中查找当前字符串是否出现过
 2 #include <iostream>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <string>
 8 #include <deque>
 9 #include <map>
10 #define INF 0x3f3f3f3f
11 #define FRE() freopen("in.txt","r",stdin)
12 
13 using namespace std;
14 typedef long long ll;
15 const int maxn = 1e5 + 5;
16 int tot,n;
17 int trie[maxn][26];
18 //bool isw[maxn];//查询整个单词用
19 
20 void Insert(char* str, int rt)
21 {
22     for(int i = 0; str[i]; i++)
23     {
24         int x = str[i] - a;
25         if(trie[rt][x] == 0)//当前插入的字母在之前同一节点处没有出现过
26         {
27             trie[rt][x] = tot++;//字母出入新位置,否则不作处理
28         }
29         rt = trie[rt][x];//为下一个字母的插入做准备
30     }
31     //isw[rt] = true;//标志该单词最后字母的尾节点,在查询整个单词时用到
32 }
33 
34 
35 bool _Find(char *str,int rt)
36 {
37     for(int i = 0; str[i]; i++)
38     {
39         int x = str[i] - a;
40         if(trie[rt][x] == 0) return false;//以rt为头结点的x字母不存在,返回0
41         rt = trie[rt][x];//为查询下个字母做准备
42     }
43     return true;
44 }
45 
46 
47 char s[22];
48 int main()
49 {
50     tot = 0;
51     int rt = 1;
52     scanf("%d",&n);
53     for(int i = 1; i <= n; i++)
54     {
55         scanf("%s",s);
56         Insert(s, rt);
57     }
58     scanf("%d",&n);
59     for(int i = 1; i <= n; i++)
60     {
61         scanf("%s",&s);
62         if(_Find(s, rt))
63             printf("YES\n");
64         else
65             printf("NO\n");
66     }
67     return 0;
68 }
  • 统计前缀和出现的次数
 1 //查询前缀出现的次数
 2 #include <iostream>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <string>
 8 #include <deque>
 9 #include <map>
10 #define INF 0x3f3f3f3f
11 #define FRE() freopen("in.txt","r",stdin)
12 
13 using namespace std;
14 typedef long long ll;
15 const int maxn = 1e5 + 5;
16 int trie[400001][26],len,root,tot,sum[400001];
17 bool p;
18 int n,m; 
19 char s[11];
20 
21 void insert()
22 {
23     len=strlen(s);
24     root=0;
25     for(int i=0;i<len;i++)
26     {
27         int id=s[i]-a;
28         if(!trie[root][id]) trie[root][id]=++tot;
29         sum[trie[root][id]]++;//前缀后移一个位置保存 
30         root=trie[root][id];
31     }
32 }
33 int search()
34 {
35     root=0;
36     len=strlen(s);
37     for(int i=0;i<len;i++)
38     {
39         int id=s[i]-a;
40         if(!trie[root][id]) return 0;
41         root=trie[root][id];
42     }//root经过此循环后变成前缀最后一个字母所在位置的后一个位置 
43     return sum[root];//因为前缀后移了一个保存,所以此时的sum[root]就是要求的前缀出现的次数 
44 }
45 int main()
46 {
47     scanf("%d",&n);
48     for(int i=1;i<=n;i++)
49     {
50         cin>>s;
51         insert();
52     }
53     scanf("%d",&m);
54     for(int i=1;i<=m;i++)
55     {
56         cin>s;
57         printf("%d\n",search());
58     }
59 }

 

字典树模板

标签:space   ++   span   code   查询   open   names   ios   for   

原文地址:https://www.cnblogs.com/sykline/p/9737823.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!