题意:给你一个长度为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. 测试工具使用-Qunit单元测试使用过程

    031302620 应课程要求写一篇单元测试工具的博客,但是暂时没用到java,所以不想使用junit(对各种类都不熟悉的也不好谈什么测试),原计划是要用phpunit,但是安装经历了三个小时,查阅各 ...

  2. BZOJ1390 CEOI2008 Fences 凸包、Floyd最小环/DP

    传送门 为了方便描述把固定点叫做白色点,Tree叫做黑色点 一种基于特殊性质的做法: 如果不算入选白色的权值,那么一定会选中所有白色点构成的凸包上的点,因为能够尽可能围更多的黑色点.然后我们在这个基础 ...

  3. GitHub Or Subversion

    上一次转载了介绍GitHub的博文点我,我想对于初学GitHub的同学们还是有不清楚的地方,毕竟有些概念的理解比较费力.我觉得作为一个对于配置库技术已经有一定基础的同学们,要学习GitHub,最快以及 ...

  4. 【译】参考手册-React组件

    react version: 15.4.2 React.Component 组件能够让你将UI拆分为多个独立自治并可重用的部分.在 React 中提供了 React.Component. 概述 Rea ...

  5. RuntimeError: Python is not installed as a framework.

    RuntimeError: Python is not installed as a framework. 文章转载:https://www.cnblogs.com/harelion/p/563776 ...

  6. 避免使用HttpClient的系统代理

    这两天在玩Consul, 他的.Net驱动使用了HttpClient来发送Http请求. 但是我的电脑上装有SS, 所以请求会被SS过滤一次, 然后导致请求的延迟一直比较高. 然后只需要改写一下Htt ...

  7. 初识Python-1

    1,计算机基础. 2,python历史. 宏观上:python2 与 python3 区别: python2 源码不标准,混乱,重复代码太多, python3 统一 标准,去除重复代码. 3,pyth ...

  8. es6在网页中模块引入的方法

    前言: 以前,当然包括现在的大部分js引入,我们都是利用<script></script>这种全局的方式进行引入,当然这种弊端还是用的,比如这样直接利用script引入的话,会 ...

  9. jabRef里引用的相邻同名作者变横线

    用jabRef引用同名作者的文章时,出现了第二个文章的作者变成了横线,在搜了相关资料后,发现作如下修改可避免: 1.在.bib文件中加入开关,并修改默认配置: @IEEEtranBSTCTL{IEEE ...

  10. C#复习笔记(4)--C#3:革新写代码的方式(Lambda表达式和表达式树)

    Lambda表达式和表达式树 先放一张委托转换的进化图 看一看到lambda简化了委托的使用. lambda可以隐式的转换成委托或者表达式树.转换成委托的话如下面的代码: Func<string ...