HDU 5384 字典树、AC自动机
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5384
用字典树、AC自动机两种做法都可以做
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
using namespace std;
struct node{
int cnt;
node *next[];
node(){
cnt = ;
for(int i = ;i<;i++)
next[i] = NULL;
}
}*a;
void insert(char *str){
node *head = a;
int len = strlen(str);
for(int i = ;i<len;i++){
int temp = str[i]-'a';
if(head->next[temp] == NULL)
head->next[temp] = new node;
head = head->next[temp];
}
head->cnt++;
}
int find(string t){
node *head = a;
int ans = ;
for(int i = ;i<t.size();i++){
if(head->next[t[i]-'a']){
if(head->next[t[i]-'a']->cnt){
ans++; }
}else
break;
head = head->next[t[i]-'a'];
}
head->cnt--;
return ans;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
a = new node;
int n;
scanf("%d",&n);
char s[];
for(int i = ;i<n;i++){
scanf(" %s",s);
insert(s);
}
string des;
cin >> des;
int ans = ;
for(int i = ;i<des.size();i++){
ans += find(des.substr(i));
}
printf("%d\n",ans);
}
return ;
}
字典树
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
using namespace std;
struct node{
int cnt;
node *next[];
node(){
cnt = ;
for(int i = ;i<;i++)
next[i] = NULL;
}
}*a;
void insert(char *str){
node *head = a;
int len = strlen(str);
for(int i = ;i<len;i++){
int temp = str[i]-'a';
if(head->next[temp] == NULL)
head->next[temp] = new node;
head = head->next[temp];
}
head->cnt++;
}
int find(string t){
node *head = a;
int ans = ;
for(int i = ;i<t.size();i++){
if(head->next[t[i]-'a']){
if(head->next[t[i]-'a']->cnt){
ans++; }
}else
break;
head = head->next[t[i]-'a'];
}
head->cnt--;
return ans;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
a = new node;
int n;
scanf("%d",&n);
char s[];
for(int i = ;i<n;i++){
scanf(" %s",s);
insert(s);
}
string des;
cin >> des;
int ans = ;
for(int i = ;i<des.size();i++){
ans += find(des.substr(i));
}
printf("%d\n",ans);
}
return ;
}
AC自动机
HDU 5384 字典树、AC自动机的更多相关文章
- 字典树&&AC自动机---看完大概应该懂了吧。。。。
目录 字典树 AC自动机 字典树 又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计 ...
- HDU 5384——Danganronpa——————【AC自动机】
Danganronpa Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Tot ...
- hdu 2896 病毒侵袭 ac自动机
/* hdu 2896 病毒侵袭 ac自动机 从题意得知,模式串中没有重复的串出现,所以结构体中可以将last[](后缀链接)数组去掉 last[]数组主要是记录具有相同后缀模式串的末尾节点编号 .本 ...
- 2021.11.09 P2292 [HNOI2004]L语言(trie树+AC自动机)
2021.11.09 P2292 [HNOI2004]L语言(trie树+AC自动机) https://www.luogu.com.cn/problem/P2292 题意: 标点符号的出现晚于文字的出 ...
- hdu 2896 病毒侵袭 AC自动机(查找包含哪些子串)
病毒侵袭 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- hdu 4117 -- GRE Words (AC自动机+线段树)
题目链接 problem Recently George is preparing for the Graduate Record Examinations (GRE for short). Obvi ...
- HDU 6096 String(AC自动机+树状数组)
题意 给定 \(n\) 个单词,\(q\) 个询问,每个询问包含两个串 \(s_1,s_2\),询问有多少个单词以 \(s_1\) 为前缀, \(s_2\) 为后缀,前后缀不能重叠. \(1 \leq ...
- HDU 5164Matching on Array(AC自动机)
这是BC上的一道题,当时比赛没有做,回头看看题解,说是AC自动机,想着没有写过AC自动机,于是便试着抄抄白书的模板,硬是搞了我数个小时2000ms时限1800过了= = ! 这里就直接贴上BC的结题报 ...
- 小菜鸟 菜谈 KMP->字典树->AC自动机->trie 图 (改进与不改进)
本文的主要宗旨是总结自己看了大佬们对AC自动机和trie 图 的一些理解与看法.(前沿:本人水平有限,总结有误,希望大佬们可以指出) KMP分割线--------------------------- ...
随机推荐
- CF570D:Tree Requests
传送门 DFS重标号+二分 打比赛的时候想了很多方法..DFS序,BFS序,倍增什么的都考虑了一遍,但是几乎要么是可以维护两个区间但是代码复杂度爆炸,要么就是只能维护单一维度的信息. 这道题的具体做法 ...
- Nginx系列4之基础配置
preface 公司跑得大多数是LNMP平台,所以借此机会多多了解下Nginx.下面主要说说Nginx下面几点: 防盗链 日志切割 负载均衡 虚拟主机 location匹配解释 substatus状态 ...
- 有return的情况下try catch finally的执行顺序
结论:1.不管有木有出现异常,finally块中代码都会执行:2.当try和catch中有return时,finally仍然会执行:3.finally是在return后面的表达式运算后执行的(此时并没 ...
- javascript中对象字面量的理解
javascript中对象字面量与数组字面量 第一部分 我们知道JavaScript中的数据类型有基本数据类型和引用类型,其中Object类型就是非常常用的类型.那么如果创建一个Object类型的实例 ...
- WinForm------Reflector反编译工具下载
地址: http://www.ddooo.com/softdown/70642.htm
- VIM for Python and Django Development
VIM for Python and Django Development VIM-PyDjango created by Programmer for Programmer who work on ...
- vm centos 添加网卡 无配置文件
vm centos 添加网卡 无配置文件 解决办法 [root@test ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:C8:41:FB ...
- JavaWeb学习笔记——Ajax
- Java排序算法——归并排序
import java.util.Arrays; //================================================= // File Name : MergeSor ...
- Ios 消息推送
手把手教你做iOS推送 http://www.cocoachina.com/industry/20130321/5862.html http://www.cnblogs.com/cdts_change ...