题意:给你一个长度为n的单词表,一个文本串,问你这个文本串中出现了单词表中多少个单词;

解题思路:ac自动机的模板题,可以直接当模板用;

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=1005000;
struct node
{
node *next[27];
node *fail;
int sum;
};
char t[100];
char s[1000500];
int cnt;
node *root;
node *que[maxn];
int head,tail;
node* newnode()
{
node *p=new node;
for(int i=0;i<26;i++)
p->next[i]=NULL;
p->sum=0;p->fail=0;
return p;
}
void build_trie(char *s)
{
node *p=root;
int slen=strlen(s);
for(int i=0;i<slen;i++)
{
int id=s[i]-'a';
if(p->next[id]==NULL)
{
p->next[id]=newnode();
}
p=p->next[id];
}
p->sum++;
}
void build_fail()
{
head=0;
tail=1;
que[head]=root;
node *p;
node *temp;
while(head<tail)
{
temp=que[head++];
for(int i=0;i<26;i++)
{
if(temp->next[i])
{
if(temp==root)
{
temp->next[i]->fail=root;//root的儿子的fail全部指向root;
}
else
{
//找temp儿子的指针;
p=temp->fail;//temp的指向;
while(p)
{
if(p->next[i])//如果temp的指向存在且和儿子相同,儿子指向为这个;
{
temp->next[i]->fail=p->next[i];
break;
}
p=p->fail;
}
if(p==NULL)
temp->next[i]->fail=root;//儿子指向根节点;
}
que[tail++]=temp->next[i];
}
}
}
}
void ac_automation(char *s)
{
node *p=root;
int slen=strlen(s);
for(int i=0;i<slen;i++)
{
int id=s[i]-'a';
while(!p->next[id]&&p!=root)
p=p->fail;
p=p->next[id];
if(!p)
p=root;
node *temp=p;
while(temp!=root)
{ if((temp->sum)>=0)
{
cnt+=temp->sum;
temp->sum=-1;
}
else
break;
temp=temp->fail;
}
}
}
void Delete(node *&top)
{
if(top==NULL)
return;
for(int i=0;i<26;i++)
Delete(top->next[i]);
delete top;
}
int main()
{
int tt;
int n;
scanf("%d",&tt);
while(tt--)
{
root=newnode();
scanf("%d",&n);
while(n--)
{
scanf("%s",t);
build_trie(t);
}
scanf("%s",s);
cnt=0;
build_fail();
ac_automation(s);
Delete(root);
printf("%d\n",cnt);
}
}

  附上非指针版本:

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int maxn=1000500;
const int N=500050;
int trie[N][26],fail[N],tot;
int sum[N];
bool visit[N];
char s[maxn];
char t[60];
void build_trie(char *str)
{
int root=0;
int slen=strlen(str);
for(int i=0;i<slen;i++)
{
int id=str[i]-'a';
if(!trie[root][id])
{
trie[root][id]=++tot;
}
root=trie[root][id];
}
sum[root]++;
}
void build_fail()
{
queue<int>q;
for(int i=0;i<26;i++)
{
if(trie[0][i]!=0)
q.push(trie[0][i]);
}
while(!q.empty())
{
int now=q.front();q.pop();
for(int i=0;i<26;i++)
{
if(!trie[now][i])
{
trie[now][i]=trie[fail[now]][i];
continue;
}
fail[trie[now][i]]=trie[fail[now]][i];
q.push(trie[now][i]);
}
}
}
int ac(char *str)
{
int root=0,ans=0;
int slen=strlen(str);
for(int i=0;i<slen;i++)
{
visit[root]=1;
int id=str[i]-'a';
int y=trie[root][id];
while(y&&!visit[y])
{
visit[y]=1;
if(sum[y])
{
ans=ans+sum[y];
sum[y]=0;
}
y=fail[y];
}
root=trie[root][id];
}
return ans;
}
void init()
{
memset(trie,0,sizeof(trie));
memset(sum,0,sizeof(sum));
memset(visit,0,sizeof(visit));
memset(fail,0,sizeof(fail));
tot=0;
}
int main()
{
int n;
int tt;
scanf("%d",&tt);
while(tt--)
{
init();
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",t);
build_trie(t);
}
build_fail();
scanf("%s",s);
printf("%d\n",ac(s));
}
}

  

hdu-2222(ac自动机模板)的更多相关文章

  1. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  2. HDU 2222 & ac自动机模板

    题意: 求n个模板串在匹配串中出现了几个. SOL: 反正就是模板啦...似乎比KMP都简单----这么说似乎有点不道德...毕竟先看的KMP而他们并没有什么不同... 貌似自己的理解和他们画的图还是 ...

  3. HDU 3065 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...

  4. HDU 2896 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...

  5. HDU 2222 (AC自动机)

    HDU 2222 Keywords search Problem : 给若干个模式串,询问目标串中出现了多少个模式串. Solution : 复习了一下AC自动机.需要注意AC自动机中的fail,和n ...

  6. HDU 2222 ----AC自动机

    Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...

  7. HDU 2222 AC自动机 裸题

    题意: 问母串中出现多少个模式串 注意ac自动机的节点总数 #include <stdio.h> #include <string.h> #include <queue& ...

  8. HDU 2222 AC自动机模版题

    所学的AC自动机都源于斌哥和昀神的想法. 题意:求目标串中出现了几个模式串. 使用一个int型的end数组记录,查询一次. #include <cstdio> #include <c ...

  9. hdu 2222(AC自动机模版题)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  10. hdu 2222 ac自动机更新模板 for onSite contest

    http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 #include <cstdio> #include <cstdlib> ...

随机推荐

  1. 【php增删改查实例】第二十四节 - 文件上传在项目中的具体应用

    文件上传在项目中,一般有两个用武之地,分别为设置用户的头像和上传附件.本节我们演示如果进行用户头像的上传. 因为一个用户单独并且唯一对应了一个头像,是一对一的关系,所以我们需要去给tm_users表添 ...

  2. ASP.NET Core 与支付宝开发文档

    一.目录 ASP.NET Core 2.0 使用支付宝PC网站支付 ASP.NET Core 2.0 支付宝当面付之扫码支付 常见使用问题解答 已有多个公司数个项目用本组件并上线,稳定使用. 二.项目 ...

  3. 有一个IT男友是怎么样体验呢?

      前言:          一提到IT男,大家就会惯性的给他贴上无趣,情商低,屌丝,技术宅的的等标签.那么作为一个IT男的女友到底是什么样的体验呢? 礼物:         别人的男朋友送礼物,一般 ...

  4. Nginx 通过 Lua + Redis 实现动态封禁 IP

    一.背景 为了封禁某些爬虫或者恶意用户对服务器的请求,我们需要建立一个动态的 IP 黑名单.对于黑名单之内的 IP ,拒绝提供服务. 二.架构 实现 IP 黑名单的功能有很多途径: 1.在操作系统层面 ...

  5. hibernate添加数据时Exception in thread "main" org.hibernate.PropertyValueException: not-null property references a null or transient value: com.javakc.hibernate.test.entity.TestEntity.testName

    意思是,一个非null属性引用了一个null或瞬态值.就是在对应实体类配置文件hbm.xml中该属性配置了not-null="true",将其去掉即可.

  6. 安装配置JDK和Eclipse的步骤

    导读 作为Java程序员,需要在Linux系统上安装Eclipse,很多人不知要如何安装,在安装Eclipse前,还需安装JDK,Linux下如何安装JDK和Eclipse呢?下面跟朋友们介绍下Lin ...

  7. Day10 Python基础之特殊函数(八)

    一些特殊函数 1.递归函数(recursion) 递归函数的定义:在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. 递归函数的优点:是定义简单,逻辑清晰.理论上,所 ...

  8. Python并发编程

    进程 相关概念 进程 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.在早期面向进程设计的计算机结构中,进程是程序的基本 ...

  9. bat 文本合并

    小工具—把多个TXT文件合成一个 - TTXS_RS的博客 - CSDN博客https://blog.csdn.net/TTXS_RS/article/details/79743384 把所有文本文件 ...

  10. 分布式ID生成系统 UUID与雪花(snowflake)算法

    Leaf——美团点评分布式ID生成系统 -https://tech.meituan.com/MT_Leaf.html 网游服务器中的GUID(唯一标识码)实现-基于snowflake算法-云栖社区-阿 ...