题目链接:传送门

描述
给定 $N$ 个字符串 $S_1,S_2,\cdots,S_N$,接下来进行 $M$ 次询问,每次询问给定一个字符串 $T$,求 $S_1 \sim S_N$ 中有多少个字符串是 $T$ 的前缀。输入字符串的总长度不超过 $10^6$,仅包含小写字母。

输入格式
第一行两个整数 $N,M$。接下来 $N$ 行每行一个字符串 $S_i$。接下来 $M$ 行每行一个字符串表示询问。

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

样例输入
3 2
ab
bc
abc
abc
efg

样例输出
2
0

模板(一个最最普通的字典树模板):

namespace Trie
{
const int SIZE=maxn*;
int sz;
struct TrieNode{
int ed;
int nxt[];
}trie[SIZE];
void init()
{
sz=;
memset(trie,,sizeof(trie));
}
void insert(const string& s)
{
int p=;
for(int i=;i<s.size();i++)
{
int ch=s[i]-'a';
if(!trie[p].nxt[ch]) trie[p].nxt[ch]=++sz;
p=trie[p].nxt[ch];
}
trie[p].ed++;
}
int search(const string& s)
{
int p=;
for(int i=;i<s.size();i++)
{
p=trie[p].nxt[s[i]-'a'];
if(!p) return ;
}
return trie[p].ed;
}
};

题解:

字典树板子题,对上面的板子稍作修改即可。

AC代码:

#include<bits/stdc++.h>
using namespace std; namespace Trie
{
const int SIZE=1e6+;
int sz;
struct TrieNode{
int ed;
int nxt[];
}trie[SIZE];
void init()
{
sz=;
memset(trie,,sizeof(trie));
}
void insert(const string& s)
{
int p=;
for(int i=;i<s.size();i++)
{
int ch=s[i]-'a';
if(!trie[p].nxt[ch]) trie[p].nxt[ch]=++sz;
p=trie[p].nxt[ch];
}
trie[p].ed++;
}
int search(const string& s)
{
int res=;
int p=;
for(int i=;i<s.size();i++)
{
p=trie[p].nxt[s[i]-'a'];
res+=trie[p].ed;
if(!p) break;
}
return res;
}
}; int n,m;
string s;
int main()
{
cin>>n>>m;
Trie::init();
for(int i=;i<=n;i++)
{
cin>>s;
Trie::insert(s);
}
for(int i=;i<=m;i++)
{
cin>>s;
cout<<Trie::search(s)<<endl;
}
}

CH 1601 - 前缀统计 - [字典树模板题]的更多相关文章

  1. 字典树模板题(统计难题 HDU - 1251)

    https://vjudge.net/problem/HDU-1251 标准的字典树模板题: 也注意一下输入方法: #include<iostream> #include<cstdi ...

  2. HDU 1251 统计难题(字典树模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给出一些单词,然后有多次询问,每次输出以该单词为前缀的单词的数量. 思路: 字典树入门题. #inc ...

  3. HDU - 1251 字典树模板题

    Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).  Input输入数据的第一部 ...

  4. AcWing 142. 前缀统计 字典树打卡

    给定N个字符串S1,S2…SNS1,S2…SN,接下来进行M次询问,每次询问给定一个字符串T,求S1S1-SNSN中有多少个字符串是T的前缀. 输入字符串的总长度不超过106106,仅包含小写字母. ...

  5. HDU 2072 - 单词数 - [(有点小坑的)字典树模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2072 Problem Descriptionlily的好朋友xiaoou333最近很空,他想了一件没有 ...

  6. (模板)hdoj1251(字典树模板题)

    题目链接:https://vjudge.net/problem/HDU-1251 题意:给定一系列字符串之后,再给定一系列前缀,对每个前缀查询以该字符串为前缀的字符串个数. 思路: 今天开始学字典树, ...

  7. P1184 高手之在一起(字典树模板题,hash算法, map)

    哎,唯一值得说明的是,这道题的输入有bug 先把字典树的算法模板放一下 #include<iostream> #include<cstring> using namespace ...

  8. POJ3630-Phone List-Trie字典树模板题

    Given a list of phone numbers, determine if it is consistent in the sense that no number is the pref ...

  9. hdu 1251 字典树模板题 ---多串 查找单词出现次数

    这道题题目里没有给定数据范围 我开了2005  疯狂的WA 然后开了50000, A掉  我以为自己模板理解错  然后一天没吃饭,饿得胃疼还是想着把这题A掉再去吃,谁知竟然是这样的问题,,,呵呵~~~ ...

随机推荐

  1. java 字符串中参数化符号${}的解析

    我们在很多地方都能看到代表参数意义的符号${},可能我们在写一些框架的时候,有时候也需要用到这个符号,但他们是如何精确解析的?或者说需要我们自已写的时候,如何写?我们先来看以下的几个场景: 1.字符串 ...

  2. Spring Boot 2.0 入门指南

    0x01 什么是Spring Boot? Spring Boot是用来简化Spring应用初始搭建以及开发过程的全新框架,被认为是Spring MVC的“接班人”,和微服务紧密联系在一起. 0x02 ...

  3. vue使用方法计算总金额

    1.预览 2.index.html <!DOCTYPE html> <html lang="en"> <head> <meta chars ...

  4. Haproxy全透明代理

    1. 系统环境搭建 操作系统Centos7 内核版本3.10 Centos7已自带TPROXY模块,不需要安装TPROXY 2. Haproxy下载,编译,安装,配置 下载地址 http://www. ...

  5. Linux免密码登录设置 && 设置快捷键

    看到这篇文章,你肯定是有这种需求. 假设要登录的机器为192.168.1.100,当前登录的机器为192.168.1.101. 首先在101的机器上生成密钥(如果已经生成可以跳过): $ ssh-ke ...

  6. anaconda的kernel对jupyter可见

    在anaconda的kernel下,安装nb_conda_kernels conda install nb_conda_kernels 参考这篇博客

  7. halcon模板匹配

    在机器视觉应用中,经常需要对图像进行仿射变换.1.在基于参考的视觉检测中,由于待检图像与参考图像或多或少都会存在几何变化(平移.旋转.缩放等),所以在做比较之前一般都要对待检图像进行仿射变换以对齐图像 ...

  8. Go指南练习_Stringer

    源地址 https://tour.go-zh.org/methods/18 一.题目描述 通过让 IPAddr 类型实现 fmt.Stringer 来打印点号分隔的地址. 例如,IPAddr{1, 2 ...

  9. centos7 防火墙

    1.firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: systemctl status f ...

  10. [UI] 01 - CSS

    前言 一.认识 From: http://www.runoob.com/css/css-tutorial.html CSS 指层叠样式表 (Cascading Style Sheets) 解决内容与表 ...