AcWing:142. 前缀统计(字典树)
给定N个字符串S1,S2…SNS1,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+; int tree[maxn][];
char str[maxn];
int End[maxn];
int tot = ; void insert(char *str) {
int len = strlen(str);
int root = ;
for(int i = ; i < len; i++) {
int idx = str[i] - 'a';
if(tree[root][idx] == ) {
tree[root][idx] = ++tot;
}
root = tree[root][idx];
}
End[root]++; //记录每个单词的个数
} int search(char *str) {
int len = strlen(str);
int root = ;
int ans = ;
for(int i = ; i < len; i++) {
int idx = str[i] - 'a';
if(tree[root][idx] == ) {
break;
}
root = tree[root][idx];
ans += End[root];
}
return ans;
} int main() {
int n, m;
scanf("%d %d", &n, &m);
for(int i = ; i < n; i++) {
scanf("%s", str);
insert(str);
}
while(m--) {
scanf("%s", str);
printf("%d\n", search(str));
}
return ;
}
AcWing:142. 前缀统计(字典树)的更多相关文章
- AcWing 142. 前缀统计 字典树打卡
给定N个字符串S1,S2…SNS1,S2…SN,接下来进行M次询问,每次询问给定一个字符串T,求S1S1-SNSN中有多少个字符串是T的前缀. 输入字符串的总长度不超过106106,仅包含小写字母. ...
- CH 1601 - 前缀统计 - [字典树模板题]
题目链接:传送门 描述给定 $N$ 个字符串 $S_1,S_2,\cdots,S_N$,接下来进行 $M$ 次询问,每次询问给定一个字符串 $T$,求 $S_1 \sim S_N$ 中有多少个字符串是 ...
- POJ 3764 The xor-longest( 树上异或前缀和&字典树求最大异或)
In an edge-weighted tree, the xor-length of a path p is defined as the xor sum of the weights of edg ...
- HDU5790 Prefix 字典树+主席树
分析:这个题和spoj的d_query是一个题,那个是求一段区间里有多少个不同的数字,这里是统计有多少个不同的前缀 用字典树进行判重,(和查询不同的数字一样)对于每个不同的前缀,只保留它最后一次出现的 ...
- Good Firewall(字典树 HDU4760)
Good Firewall Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- Barty's Computer 字典树
https://nanti.jisuanke.com/t/17122 Barty have a computer, it can do these two things. Add a new stri ...
- hdoj4099(字典树+高精度)
题目链接:https://vjudge.net/problem/HDU-4099 题意:给T组询问,每个询问为一个字符串(长度<=40),求以该字符串为开始的fibonacci数列的第一个元素的 ...
- HDU 1251 统计难题 (字典树)(查询是否为前缀)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- HDU 1671 (字典树统计是否有前缀)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671 Problem Description Given a list of phone number ...
随机推荐
- Asp.net core Identity + identity server + angular + odata + 权限管理
今天踩了一个坑. 网站发布后看到了一个 error Refused to execute inline script because it violates the following Content ...
- lombok 注解
lombok 注解 1. 什么是 lombok 注解 Lombok 是一种 Java™ 实用工具,可用来帮助开发人员消除 Java 的冗长,尤其是对于简单的 Java 对象(POJO).它通过注解实现 ...
- JS基础_条件运算符
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- redis cluster突然少了一个node的问题
今天进入redis执行cluster info发现 cluster_state:fail 并且 cluster_known_nodes:5 少了一个7006的node 然后我重启了7006的 ...
- java enum类自定义属性
enum类自定义属性 这就是enum比static静态变量好用的地方了,可以赋予每一个枚举值若干个属性,例如 实例1: public enum GasStationChannel { ZH(" ...
- ES6新增内容总结
ES6新增内容有:1,模块化思想.2,关于变量let和const.3,解构赋值.4,字符串的扩展.5,函数的扩展.6,箭头函数.7,继承apply的用法 以下就是详解: 1:模块化思想 非模块化有命名 ...
- 非JAVA WEB项目提供Http接口调用实现
package com.monitor.app.utils; import com.alibaba.fastjson.JSON; import com.google.gson.Gson; import ...
- Python自制小时钟,并转换为exe可执行程序详解
一,简介Python写完程序,要靠命令来执行太LOW,太低调了,还不华丽了. 再说别人的电脑,都没有Python库,怎么执行,还能不能愉快的一起玩耍了. 所以哪怕只会写一个HelloWorld,也要弄 ...
- GDI+ 绘图教程 验证码
使用的 C# winform using System; using System.Collections.Generic; using System.ComponentModel; using Sy ...
- JavaScript事件的基本学习