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分割线--------------------------- ...
随机推荐
- javascript变量、作用域和内存问题......
1基本类型是指那些保存在栈内存的简单数据段,引用类型是指那些保存在堆内存中的对象,变量中保存的实际上只是一个指针. 2javascript中5种基本数据类型Undefined,Null,Boolean ...
- Altium Designer 15 --- Nets Update
Now I want to introduce the use of 'Configure Physical Nets', as follows: If you has finished the PC ...
- uC/OS-II时间(OS_time)块
/*************************************************************************************************** ...
- xfce4 dev tools的一些说明
xfce4 dev tools实际上基本是封装了一些autoconf的宏函数 比如XDT_I18N: AC_DEFUN([XDT_I18N], [ dnl Substitute GETTEXT_PAC ...
- 以sysdba身份登录oracle报ORA-1031权限不足错误之完美分析
在linux 操作系统的数据库服务器上,使用”sqlplus / as sysdba” 登录Oracle 10.2 数据库实例时,登录失败,显示ORA-01031: 权限不足. 在数据库所在服务器上 ...
- PHP----Ajax异步请求
需要两个PHP页面:1.php是发出请求和接受请求结果的.2.php是处理请求的结果. 1.php中代码: <a href="#" onclick="sendAja ...
- Unity Serialization
http://forum.unity3d.com/threads/serialization-best-practices-megapost.155352/ http://docs.unity3d.c ...
- Beyond Compare for mac 无限试用方法
1.在官网(http://www.scootersoftware.com/download.php)下载最新的 beyond compare. 2.解压后, 把 beyond compare 复制到应 ...
- PHP代码质量优化
最近总结了一些平常写PHP代码时的一些优化分享给大家. 1.尽量使用绝对路径 相对路径中会检查很多路径,这时我们可以使用绝对路径,但绝对路径不利于后期维护,所以define定义的时候使用__FILE_ ...
- 序列化与反序列化成XML
http://blog.itpub.net/12639172/viewspace-490786/ 现在XML都普遍的用到了很多地方,它的平台无关.方便.结构化.适用性的特点让人不得不去接受它,在C#中 ...