• AcWing:142. 前缀统计(字典树)


    给定N个字符串S1,S2SNS1,S2…SN,接下来进行M次询问,每次询问给定一个字符串T,求S1S1~SNSN中有多少个字符串是T的前缀。

    输入字符串的总长度不超过106106,仅包含小写字母。

    输入格式

    第一行输入两个整数N,M。

    接下来N行每行输入一个字符串SiSi。

    接下来M行每行一个字符串T用以询问。

    输出格式

    对于每个询问,输出一个整数表示答案。

    每个答案占一行。

    输入样例:

    3 2
    ab
    bc
    abc
    abc
    efg
    

    输出样例:

    2
    0
    
    算法:字典树
     
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    
    using namespace std;
    
    const int maxn = 1e6+7;
    
    int tree[maxn][26];
    char str[maxn];
    int End[maxn];
    int tot = 1;
    
    void insert(char *str) {
        int len = strlen(str);
        int root = 1;
        for(int i = 0; i < len; i++) {
            int idx = str[i] - 'a';
            if(tree[root][idx] == 0) {
                tree[root][idx] = ++tot;
            }
            root = tree[root][idx];
        }
        End[root]++;       //记录每个单词的个数
    }
    
    int search(char *str) {
        int len = strlen(str);
        int root = 1;
        int ans = 0;
        for(int i = 0; i < len; i++) {
            int idx = str[i] - 'a';
            if(tree[root][idx] == 0) {
                break;
            }
            root = tree[root][idx];
            ans += End[root];
        }
        return ans;
    }
    
    int main() {
        int n, m;
        scanf("%d %d", &n, &m);
        for(int i = 0; i < n; i++) {
            scanf("%s", str);
            insert(str);
        }
        while(m--) {
            scanf("%s", str);
            printf("%d
    ", search(str));
        }
        return 0;
    }
  • 相关阅读:
    第一篇:spring boot 初始
    数据结构 -- 线段树
    数据结构 -- 优先队列和堆排序
    javaIO -- 流的体系设计思路、基础分类
    JavaIO -- Reader 和 Writer
    javaIO -- InputStream和OutStream
    javaIO -- File源码
    数据结构 -- 二叉树(Binary Search Tree)
    数据结构 -- 链表(LinkedList)
    数据结构 -- 栈(Stack)
  • 原文地址:https://www.cnblogs.com/buhuiflydepig/p/11305623.html
Copyright © 2020-2023  润新知