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分割线--------------------------- ...
随机推荐
- JS性能方面--内存管理及ECMAScript5 Object的新属性方法
Delete一个Object的属性会让此对象变慢(多耗费15倍的内存) var o = { x: 'y' }; delete o.x; //此时o会成一个慢对象 o.x; // var o = { x ...
- BZOJ2186: [Sdoi2008]沙拉公主的困惑
传送门 常规数论题,利用欧拉函数的相关性质. 题求$[1,N!]$中与$M!$互质的数的个数,且$M \leq N$.然后根据欧拉函数的相关性质很容易得出这道题的答案为$\frac{\phi (M!) ...
- Altium Designer 15 --- Design PCB Frame by Rhinoceros
step 1: Draw a PCB shape and the main component placed in the PCB. The drawing sheet should be in th ...
- JavaWeb学习笔记——JavaEE基础知识
- hdu 1318 Palindromes
Palindromes Time Limit:3000MS Memory Limit:0KB 64bit ...
- Django笔记-登陆、注册(利用cookie实现)
1.项目结构: 2.关键代码: settings.py INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'djang ...
- centos 7.0 查看根目录下所有文件夹
centos 7.0最小化安装 第一行是登录 [root@localhost ~]# [root@localhost ~]# cd ../ [root@localhost /]# ls bin dev ...
- yii2-按需加载并管理CSS样式/JS脚本
原文地址:https://segmentfault.com/a/1190000003742452
- CSV文件的读取 导出
CSV文件是 逗号分隔值文件 内容基本是这么存的... 姓名,年龄,性别/r/n王有才,,男/r/n王二妞,,女 一行值内用逗号 , 分开 行与行之间用 /r/n 分隔 说白了,他其实是一个字符 ...
- PHP数组处理函数的使用array_reduce(二)
关于PHP数组操作函数更为细致的用法大家还可以参考PHP在线参考手册:http://php.net/manual/zh/index.php array_reduce — 用回调函数迭代地将数组简化为单 ...