zoukankan      html  css  js  c++  java
  • hihocoder 1014

    Trie树模板题 时隔几个月终于又get了Trie树好理解又简洁的模板 ``` /**/ #include #include #include #include #include #include #include #include #include #include #include #include

    typedef long long LL;
    typedef unsigned long long ULL;
    using namespace std;

    bool Sqrt(LL n) { return (LL)sqrt(n) * sqrt(n) == n; }
    const double PI = acos(-1.0), ESP = 1e-10;
    const LL INF = 99999999999999;
    const int inf = 999999999, N = 4e5 + 24;
    int n, m, h, root, len, cnt[N], trie[N][28];
    char s[12];

    void insert()
    {
    root = 0;
    len = strlen(s);
    for(int i = 0; i < len; i++) {
    int id = s[i] - 'a';
    if(!trie[root][id]) trie[root][id] = ++h;
    cnt[trie[root][id]]++; //Don't forget to change the counter.
    root = trie[root][id];
    }
    }

    void search()
    {
    root = 0;
    len = strlen(s);
    for(int i = 0; i < len; i++) {
    int id = s[i] - 'a';
    if(!trie[root][id]) { puts("0"); return ;}
    root = trie[root][id];
    }
    printf("%d ", cnt[root]);
    }

    int main()
    {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    scanf("%d", &n);
    while(n--) {
    scanf("%s", s);
    insert();
    }
    scanf("%d", &m);
    while(m--) {
    scanf("%s", s);
    search();
    }

    return 0;
    

    }
    /*
    input:
    output:
    modeling:
    methods:
    complexity:
    summary:
    */

  • 相关阅读:
    Mac基本操作记录
    flutter vscode+第三方安卓模拟器
    git一些基本操作
    获取点击元素的绝对位置
    windows下提交前检测操作
    VGG网络学习
    MySQL数据库中文乱码
    配深度学习环境中的小tips(tensorflow+anaconda+keras+cuda)
    Docker安装指定的anaconda
    yolov1阅读
  • 原文地址:https://www.cnblogs.com/000what/p/11713672.html
Copyright © 2011-2022 走看看