• 20190724 Trie


      放假第二天,猪排好像蛮好吃,尴尬的西餐,对面还是一个小姐姐,全程看JOJO缓解尬癌。

      HDU 2846 Repository

      复习加预习

      

    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <cmath>
    
    using namespace std;
    const int maxn = 650000;
    const int maxs = 21;
    char s[maxs];
    int trie[maxn][26];
    int tag[maxn];
    int sum[maxn];
    int tot = 1;
    void insert(char *s, int Tag) {
        int len = strlen(s), rt = 0, id;
        for (int i = 0; i < len; i++) {
            id = s[i] - 'a';
            if (trie[rt][id] == 0) {
                trie[rt][id] = tot++;
            }
            rt = trie[rt][id];
            if (tag[rt] != Tag) {
                tag[rt]  = Tag;
                sum[rt]++;
            }
        }
    }
    int find(const char *s) {
        int len = strlen(s), rt = 0, id;
        for (int i = 0; i < len; i++) {
            id = s[i] - 'a';
            if (trie[rt][id] == 0)
                return 0;
            rt = trie[rt][id];
    //        printf("rt = %d
    ", rt);
        }
        return sum[rt];
    }
    
    int main ()
    {
         int n, m;
        scanf("%d", &n);
        getchar();
        for (int i = 1; i <= n; i++) {
            scanf("%s", s);
            getchar();
            for (int j = 0; s[j]; j++) {
                insert(s+j, i);
            }
        }
          scanf("%d", &m);
        while (m--){
            scanf("%s", s);
            printf("%d
    ", find(s));
        }
        return 0;
    }
  • 相关阅读:
    UVA 712 STrees
    SGU 109 Magic of David Copperfield II
    SGU 108 Selfnumbers 2
    Go编程语言规范3表达式
    Go编程语言规范1 块,声明与作用域
    golang安装和部署
    Go编程语言规范2类型
    call web services from iPhone
    maximo 支援
    C#動態調用webservice  不加web引用
  • 原文地址:https://www.cnblogs.com/Urchin-C/p/11250276.html
Copyright © 2020-2023  润新知