hdu-2222(ac自动机模板)
题意:给你一个长度为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自动机模板)的更多相关文章
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- HDU 2222 & ac自动机模板
题意: 求n个模板串在匹配串中出现了几个. SOL: 反正就是模板啦...似乎比KMP都简单----这么说似乎有点不道德...毕竟先看的KMP而他们并没有什么不同... 貌似自己的理解和他们画的图还是 ...
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
- HDU 2222 (AC自动机)
HDU 2222 Keywords search Problem : 给若干个模式串,询问目标串中出现了多少个模式串. Solution : 复习了一下AC自动机.需要注意AC自动机中的fail,和n ...
- HDU 2222 ----AC自动机
Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...
- HDU 2222 AC自动机 裸题
题意: 问母串中出现多少个模式串 注意ac自动机的节点总数 #include <stdio.h> #include <string.h> #include <queue& ...
- HDU 2222 AC自动机模版题
所学的AC自动机都源于斌哥和昀神的想法. 题意:求目标串中出现了几个模式串. 使用一个int型的end数组记录,查询一次. #include <cstdio> #include <c ...
- hdu 2222(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu 2222 ac自动机更新模板 for onSite contest
http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 #include <cstdio> #include <cstdlib> ...
随机推荐
- 【转】WPF PasswordBox不支持绑定解决方法
原文地址:https://www.cnblogs.com/wxjing67/p/3935717.html PasswordBox的Password属性因为安全原因不支持直接绑定,可以使用依赖属性实现. ...
- 对写博客的n种思考
喜欢才能坚持 开始写博客的原因非常功利,功利到不好意思说. 反正你们也懂的,就那么几种. 问题是,如果心态一直这么功利,而写博客的前期回报几乎为零,情绪会变得沮丧,不知如何继续. 不过后来想想,其实做 ...
- CSS3下的渐变文字效果实现
如下,第一种方法已实践 一.方法一:借助mask-image属性 可以狠狠地点击这里:CSS3下的渐变文字效果方法一demo 如果您手头上的浏览器是Chrome或是Safari,则您可以在demo页面 ...
- 七、Json格式的对象都可以通过遍历来获得里面的value值
- Codeforces Round #485 (Div. 2)-B-High School: Become Human
B. High School: Become Human time limit per test 1 second memory limit per test 256 megabytes input ...
- ReentrantLock源码分析
参考: 五月的仓颉 ReentrantLock实现原理 活在梦里 AQS源码解读 重入锁是基于AQS实现的,它提供了公平锁和非公平锁两个版本的实现. public class ReentrantLoc ...
- MySQL使用select查询时,在查询结果中增加一个字段并指定固定值
假设需求是这样的: mysql> desc user; +-------+----------+------+-----+---------+----------------+ | Field ...
- ORACLE 当字段中有数据如何修改字段类型
创建视图的时候,因为表太多,里面一些字段类型不一样,PL/SQL报错,为‘表达式必须具有对应表达式相同的数据类型’,发现后,一个字段的类型为CLOB和VARCHAR2(4000)两种,将CLOB进行修 ...
- spring AOP的用法
AOP,面向切面编程,它能把与核心业务逻辑无关的散落在各处并且重复的代码给封装起来,降低了模块之间的耦合度,便于维护.具体的应用场景有:日志,权限和事务管理这些方面.可以通过一张图来理解下: Spri ...
- 本地上传项目到github
https://www.cnblogs.com/rosej/p/6056467.html(copy)