zoukankan      html  css  js  c++  java
  • Trie字符串统计-acwing

    记住一件事情即可:Trie是高效存储和查找字符串集合的数据结构

    一般来说题目是会限制字母的种类,不会太多

    #include <iostream>
    #include <cstring>
    #include <string>
    #include <cmath>
    #include <cstdio>
    #include <stdio.h>
    #include <cstdlib>
    #include <algorithm>
    #include <vector>
    #include <set>
    #include <map>
    #include <iomanip>
    #define rep(i,a,b) for(int i = a; i <= b ; i ++ )
    #define pre(i,a,b) for(int i = b ; i >= a ; i --)
    #define ll long long
    #define inf 0x3f3f3f3f
    #define ull unsigned long long
    #define ios ios::sync_with_stdio(false),cin.tie(0)
    using namespace std;
    typedef pair<int,int> PII ;
    const int N = 2e6 + 10;
    
    int son[N][26],cnt[N],idx; //下标是0的点,既是根节点,也是空节点
    char str[N];
    int n;
    void insert_string(char *str)
    {
        int root=0;
        for(int i=0;str[i];i++)
        {
            int u=str[i]-'a';
            if(!son[root][u]) son[root][u]=++idx;
            root=son[root][u];
        }
        cnt[root]++;
    }
    
    int query(char *str)
    {
        int root=0;
        for(int i=0;str[i];i++)
        {
            int u=str[i]-'a';
            if(!son[root][u]){
                return 0;
            }
            root=son[root][u];
        }
        return cnt[root];
    }
    
    int main()
    {
        ios;
        cin>>n;
        while(n--)
        {
            char op[2];
            cin>>op>>str;
            if(op[0]=='I')
            {
                insert_string(str);
            }
            else{
                cout<<query(str)<<endl;
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    elementUI form select验证问题
    Echart--基本属性
    Echart--饼状图
    Echart--折线图
    Echart--多个柱形图
    Echart--单个柱形图
    Echart--圆型图
    构析函数和构造函数
    心情
    图片
  • 原文地址:https://www.cnblogs.com/jxust-Biao/p/13338857.html
Copyright © 2011-2022 走看看