zoukankan      html  css  js  c++  java
  • 字典树模板

    • 查找该字符串是不是已经出现过
     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
    ");
    64         else
    65             printf("NO
    ");
    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
    ",search());
    58     }
    59 }
  • 相关阅读:
    [Swift]LeetCode1035.不相交的线 | Uncrossed Lines
    [Swift]LeetCode1034.边框着色 | Coloring A Border
    [Swift]LeetCode1033. 移动石子直到连续 | Moving Stones Until Consecutive
    [Swift]美人征婚问题
    [Swift]动态变化顶部状态栏(statusBar)的颜色
    [Swift-2019力扣杯春季决赛]4. 有效子数组的数目
    [Swift-2019力扣杯春季决赛]3. 最长重复子串
    [Swift-2019力扣杯春季决赛]2. 按字典序排列最小的等效字符串
    转 ORA-12638: 身份证明检索失败
    转 构建镜像
  • 原文地址:https://www.cnblogs.com/sykline/p/9737823.html
Copyright © 2011-2022 走看看