题意:给你一个长度为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. MVC5 + EF6 完整教程 (转)

    点击查看: MVC5 + EF6

  2. Linux中断管理 (3)workqueue工作队列

    目录: <Linux中断管理> <Linux中断管理 (1)Linux中断管理机制> <Linux中断管理 (2)软中断和tasklet> <Linux中断管 ...

  3. 支持“xxxContext”上下文的模型已在数据库创建后发生更改。请考虑使用 Code First 迁移更新数据库

    将项目的数据库连接用户及密码修改后(切换用户,用户名与原来不一样,用户下对象结构一致),报以下错误: 支持“XXXDBContext”上下文的模型已在数据库创建后发生更改.请考虑使用 Code Fir ...

  4. JUnit5 快速指南

    JUnit5 快速指南 version: junit5 1. 安装 2. JUnit 注解 3. 编写单元测试 3.1. 基本的单元测试类和方法 3.2. 定制测试类和方法的显示名称 3.3. 断言( ...

  5. mongodb java3.2驱动 测试 一些记录

    mongo驱动包 自带线程池的概念 获取 MongoClient mongoClient 后 通过客户端(mongoClient ) 获取 库操作 MongoDatabase 获取 表操作 Mongo ...

  6. Scala学习(九)练习

    文件正则表达式&练习 1. 编写一小段Scala代码,将某个文件中的行倒转顺序,将最后一行作为第一行,依此类推 程序代码: import scala.io.Source import java ...

  7. 【C#复习总结】垃圾回收机制(GC)1

    摘要:今天我们漫谈C#中的垃圾回收机制,本文将从垃圾回收机制的原理讲起,希望对大家有所帮助. GC的前世与今生 虽然本文是以.NET作为目标来讲述GC,但是GC的概念并非才诞生不久.早在1958年,由 ...

  8. WebClient, HttpClient, HttpWebRequest ,RestSharp之间的区别与抉择

    NETCore提供了三种不同类型用于生产的REST API: HttpWebRequest;WebClient;HttpClient,开源社区创建了另一个名为RestSharp的库.如此多的http库 ...

  9. 用友云开放平台之API网关

    本文介绍选择API网关应考虑的几方面内容,API网关在微服务框架中的作用,API网关如何选型,用友云开放平台的API网关可以做什么. 随着互联网的快速发展,当前已步入移动互联.物联网时代.企业内部系统 ...

  10. CRM系统(第一部分)

      阅读目录 1.需求分析 2.数据库表设计 3.起步 4.录入数据 5.知识点 1.需求分析 CRM客户关系管理软件---> 学员管理 用户:企业内部用户 用户量: 业务场景: 2.数据库表设 ...