题意:给你一个长度为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. WebBench压力测试工具(详细源码注释+分析)

    本文适合人群:对WebBench实现感兴趣的人 WebBench原理: Linux下使用的服务器压力测试工具,利用fork建立多个子进程,每个子进程在测试时间内不断发送请求报文,建立多个连接,然后由父 ...

  2. CentOS下安装Apache

    CentOS下安装Apache,首先在用户状态下使用su root命令切换到超级管理员界面,让后开启终端,进行apache的安装过程. [root@localhost centos]# yum ins ...

  3. macOS下appstore提示未能完成该操作的解决办法

    macOS下App Store下载软件,提示:未能完成该操作.(com.apple.commerce.client 错误 500.) 解决办法: 在终端输入 defaults write com.ap ...

  4. c# WPF RichTextBox 文字颜色

    public MainWindow() { InitializeComponent(); Run run = new Run("This is my text"); run.For ...

  5. LeetCode 905. Sort Array By Parity

    905. Sort Array By Parity Given an array A of non-negative integers, return an array consisting of a ...

  6. 重装mysql后导致Navicat连接失败

    今天重装了mysql数据库,然后再使用navicat去连接数据库的时候,一直报错 1251 Client does not support authentication protocol reques ...

  7. 009-定时关闭弹出广告窗口 By BoAi 20190414

    ;~ 定时关闭弹出广告窗口 By BoAi 20190414 ; ### 参数设置段 ######################################SingleInstance,forc ...

  8. Bus Video System CodeForces - 978E (思维)

    The busses in Berland are equipped with a video surveillance system. The system records information ...

  9. 每周分享之cookie详解

    本章从JS方向讲解cookie的使用.(实质上后端代码也是差不多用法,无非读取和设置两块) 基本用法:document.cookie="username=pengpeng"; 修改 ...

  10. Python之加密模块

    hashlib模块 加密方式以md5方式加密举例 是标准模块,直接导入即可 还有其他的加密方式,比如:.sha1()..sha224()..sha256()等,加密后的字符串长度更长,安全性更高 一. ...