题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2222

题目大意:多个模式串。问匹配串中含有多少个模式串。注意模式串有重复,所以要累计重复结果。

解题思路

AC自动机模板题。

一开始使用LRJ的坑爹静态模板,不支持重复的模式串。

在做AC自动机+DP的时候,扒了zcwwzdjn大神的动态优化(失配指向root)写法,以及借鉴了网上的AC自动机模板,

搞出了这么一个支持重复串的模板。

注意在计算last后缀链接的时候,都会修改last->cnt=0,是为了防止同一个模式串在匹配时候走了多遍。比如模:AA,匹:AAA,

修改last->cnt的结果是1,而不修改就是2。修改法在碰到HDU 2896这种需要计算多个匹配串的时候就萎了。

必须在修改之后,及时恢复。由于本身就使用了queue,所以修改时用queue记录一下哪些last被修改了,之后恢复一下就OK!

同时,修改法在HDU 3065中,统计一个模式串出现多少次也萎了。因为模式串必须要走多遍。

#include "cstdio"
#include "cstring"
#include "iostream"
#include "queue"
#include "string"
using namespace std;
struct Trie
{
Trie *next[],*fail;
int cnt;
}*root;
struct status
{
Trie *last;
int cnt;
status(Trie *last,int cnt):last(last),cnt(cnt) {}
};
Trie *newnode()
{
Trie *ret=new Trie;
memset(ret->next,,sizeof(ret->next));
ret->fail=;
ret->cnt=;
return ret;
}
void init() {root=newnode();}
void Insert(string str)
{
Trie *pos=root;
for(int i=;i<str.size();i++)
{
int c=str[i]-'a';
if(!pos->next[c]) pos->next[c]=newnode();
pos=pos->next[c];
}
pos->cnt++;
}
void getfail()
{
queue<Trie *> Q;
for(int c=;c<;c++)
{
if(root->next[c])
{
root->next[c]->fail=root;
Q.push(root->next[c]);
}
else root->next[c]=root;
}
while(!Q.empty())
{
Trie *x=Q.front();Q.pop();
for(int c=;c<;c++)
{
if(x->next[c])
{
x->next[c]->fail=x->fail->next[c];
Q.push(x->next[c]);
}
else x->next[c]=x->fail->next[c];
}
}
}
int find(string str)
{
Trie *pos=root,*last;
queue<status> Q;
int ans=;
for(int i=;i<str.size();i++)
{
int c=str[i]-'a';last;
if(pos->next[c])
{
pos=pos->next[c];
last=pos;
while(last->cnt)
{
Q.push(status(last,last->cnt));
ans+=last->cnt;
last->cnt=; //修改last->cnt
last=last->fail;
}
}
}
while(!Q.empty()) //恢复last->cnt
{
status x=Q.front();Q.pop();
x.last->cnt=x.cnt;
}
return ans;
}
int main()
{
//freopen("in.txt","r",stdin);
ios::sync_with_stdio(false);
int T,n;
string tt;
cin>>T;
while(T--)
{
cin>>n;
init();
for(int i=;i<=n;i++)
{
cin>>tt;
Insert(tt);
}
getfail();
cin>>tt;
int ans=find(tt);
printf("%d\n",ans);
}
}
11927003 2014-10-21 01:22:37 Accepted 2222 390MS 29480K 1942 B C++ Physcal

HDU 2222(AC自动机模板题)的更多相关文章

  1. HDU 2222 AC自动机模板题

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

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

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

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

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

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

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

  5. HDU 2222 AC自动机 裸题

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

  6. HDU 2222 AC自动机模版题

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

  7. HDU 2222 & ac自动机模板

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

  8. HDU 2222 AC自动机(模版题)

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

  9. Keywords Search HDU - 2222 AC自动机板子题

    In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...

随机推荐

  1. &&队友最近一周水水

    100130 练习5 5 hr ago 15.2 days Private qwerqqq 100093 DP2 16 hr ago 50.2 days Private qwerqqq 100092 ...

  2. Watering the Fields(irrigation)

    #include <cstdio> #include <algorithm> struct edge{ int f,t,w; } ed[5000000]; int pl,n,c ...

  3. VMware Snapshot 工作原理

    VMware中的快照是对VMDK在某个时间点的“拷贝”,这个“拷贝”并不是对VMDK文件的复制,而是保持磁盘文件和系统内存在该时间点的状态,以便在出现故障后虚拟机能够恢复到该时间点.如果对某个虚拟机创 ...

  4. Calico在Kubernetes中的搭建

    一,需求 Kubernetes官方推荐的是Flannel,但是Flannel是一个overlay的网络,对性能会有一定的影响.Calico恰好能解决一下overlay网络的不足. Calico在Kub ...

  5. 面向侧面的程序设计AOP-------《一》概述

    Aspect-Oriented Programming(面向方面编程,AOP)正好可以解决这一问题.它允许开发者动态地修改静态的OO模型,构造出一个能够不断增长以满足新增需求的系统,就象现实世界中的对 ...

  6. Java for LeetCode 174 Dungeon Game

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  7. CodeForces - 417B (思维题)

    Crash Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status ...

  8. p235习题1

  9. Linux中信号量处理

    参考文章: http://blog.csdn.net/qinxiongxu/article/details/7830537/ 信号量一. 什么是信号量信号量的使用主要是用来保护共享资源,使得资源在一个 ...

  10. PowerDesigner破解

    1.从百度云盘下载:我的网盘 -- 开发工具空间 -- PowerDesgner12   2.安装powerDesigner   3.将Sybase.PowerDesigner.v12.5-Crack ...