zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    MySQL-事务原理
    MySQL-索引原理
    SQL-查询前N条记录
    Shell-配置libpath环境变量
    PG-Vacuum
    python连接mysql数据库
    Spark SQL 基本操作
    spark-shell 交互式编程
    Scala统计学生成绩
    Scala模拟图形绘制
  • 原文地址:https://www.cnblogs.com/Urchin-C/p/11250276.html
Copyright © 2011-2022 走看看