题目: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自动机的更多相关文章

  1. 字典树&&AC自动机---看完大概应该懂了吧。。。。

    目录 字典树 AC自动机 字典树 又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计 ...

  2. HDU 5384——Danganronpa——————【AC自动机】

    Danganronpa Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  3. hdu 2896 病毒侵袭 ac自动机

    /* hdu 2896 病毒侵袭 ac自动机 从题意得知,模式串中没有重复的串出现,所以结构体中可以将last[](后缀链接)数组去掉 last[]数组主要是记录具有相同后缀模式串的末尾节点编号 .本 ...

  4. 2021.11.09 P2292 [HNOI2004]L语言(trie树+AC自动机)

    2021.11.09 P2292 [HNOI2004]L语言(trie树+AC自动机) https://www.luogu.com.cn/problem/P2292 题意: 标点符号的出现晚于文字的出 ...

  5. hdu 2896 病毒侵袭 AC自动机(查找包含哪些子串)

    病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  6. hdu 4117 -- GRE Words (AC自动机+线段树)

    题目链接 problem Recently George is preparing for the Graduate Record Examinations (GRE for short). Obvi ...

  7. HDU 6096 String(AC自动机+树状数组)

    题意 给定 \(n\) 个单词,\(q\) 个询问,每个询问包含两个串 \(s_1,s_2\),询问有多少个单词以 \(s_1\) 为前缀, \(s_2\) 为后缀,前后缀不能重叠. \(1 \leq ...

  8. HDU 5164Matching on Array(AC自动机)

    这是BC上的一道题,当时比赛没有做,回头看看题解,说是AC自动机,想着没有写过AC自动机,于是便试着抄抄白书的模板,硬是搞了我数个小时2000ms时限1800过了= = ! 这里就直接贴上BC的结题报 ...

  9. 小菜鸟 菜谈 KMP->字典树->AC自动机->trie 图 (改进与不改进)

    本文的主要宗旨是总结自己看了大佬们对AC自动机和trie 图 的一些理解与看法.(前沿:本人水平有限,总结有误,希望大佬们可以指出) KMP分割线--------------------------- ...

随机推荐

  1. BZOJ1047: [HAOI2007]理想的正方形

    传送门 蛤省省选果然水啊,我这种蒟蒻都能一遍A. 横向纵向维护两个单调队列,做两次求最大和最小的,总复杂度$O(NM)$ 码农题,考察代码实现能力 //BZOJ 1047 //by Cydiater ...

  2. 实现BPEL4WS演示:教程

    http://www.ibm.com/developerworks/cn/education/webservices/ws-bpelws/bpel_tutorial_cn.html 开始 什么是Bus ...

  3. BZOJ2124: 等差子序列

    题意:给一个 1 到 N 的排列{Ai},询问是否存在 1<=p1<p2<p3<p4<p5<…<pLen<=N(Len>=3),使得 Ap1,Ap ...

  4. 电脑技巧:Win8/Win10无法打开这个应用|无法使用内置管理员账户的完美解决方法

    现在装win10系统的同伴越来越多了,相比于win7,win10在某些设置方面也有些变化,比如我们在使用win8或者win10时,会碰到如图所示的对话框: Windows10/Windows8无法使用 ...

  5. Redis学习-开始

    C:\Program Files\Redis\redis-cli.exe 使用servicestack.redis class Program { static void Main(string[] ...

  6. 自然语言13_Stop words with NLTK

    https://www.pythonprogramming.net/stop-words-nltk-tutorial/?completed=/tokenizing-words-sentences-nl ...

  7. Java Web学习笔记-Servle生命周期

    Servlet会在服务器启动或第一次请求该Servlet的时候开始生命周期,在服务器停止的时候结束生命周期. 无论请求多少次Servlet,最多只有一个Servlet实例.多个客户端并发请求Servl ...

  8. ast模块

    有这么一个需求,你想从文件中读取字典,方法有很多,这里用的是ast模块 import ast with open("account","r",encoding= ...

  9. HTML5实战与剖析之触摸事件(touchstart、touchmove和touchend)(转)

    HTML5中新添加了很多事件,但是由于他们的兼容问题不是很理想,应用实战性不是太强,所以在这里基本省略,咱们只分享应用广泛兼容不错的事件,日后随着兼容情况提升以后再陆续添加分享.今天为大家介绍的事件主 ...

  10. weblogic 的安装和配置

    一.安装 1.1安装weblogic8.1 首先从www.bea.com上下载安装文件platform816_linux32.bin,然后在安装文件所在目录下键入 ./platform816_linu ...