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

题目大意:有多个文本,多个模式串。问每个模式串中,有多少个文本?(匹配可重复)

解题思路

传统AC自动机是计算单个文本中,模式串出现次数。

这里比较特殊,每个文本需要单独计算,而且每个匹配在每个文本中只能计数1次。

比如add,d只能计数1次,而不是;两次。

所以循环逐个对文本Find。每个Find里,进行Hash,保证每个匹配串只计数1次。

由于匹配串可重复,在Insert之前,也需要离散化Hash一下,把重复的下标,指向最先插入的下标。

另外,本题会卡掉不正确AC自动机模板( Find里要写成While(last!=root)而不是While(last->cnt)  )

代码

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include "cstdio"
#include "cstring"
#include "string"
#include "iostream"
#include "queue"
#include "vector"
#include "algorithm"
#include "map"
using namespace std;
#define maxn 26
int ans[],mt[];
string P[];
struct Trie
{
Trie *next[maxn],*fail;
int cnt;
}*root;
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,int index)
{
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=index;
}
void getfail()
{
queue<Trie *> Q;
for(int c=;c<maxn;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<maxn;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];
}
}
}
void Find(string str)
{
map<int,int> Hash;
Trie *pos=root,*last;
for(int i=;i<str.size();i++)
{
int c=str[i]-'a';last;
if(c<||c>maxn) {pos=root;continue;}
if(pos->next[c])
{
pos=pos->next[c];
last=pos;
while(last!=root)
{
if(!Hash.count(last->cnt))
{
ans[last->cnt]++;
Hash[last->cnt]++;
}
last=last->fail;
}
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
ios::sync_with_stdio(false);
int n,m,s;
string tmp;
while(cin>>n)
{
map<string,int> ms;
memset(ans,,sizeof(ans));
memset(mt,,sizeof(mt));
init();
for(int i=;i<=n;i++) cin>>P[i];
cin>>s;
for(int i=;i<=s;i++)
{
cin>>tmp;
if(!ms.count(tmp))
{
Insert(tmp,i);
ms[tmp]=i;
mt[i]=i;
}
else mt[i]=ms[tmp];
}
getfail();
for(int i=;i<=n;i++) Find(P[i]);
for(int i=;i<=s;i++) cout<<ans[mt[i]]<<endl;
}
}

HDU 2846 (AC自动机+多文本匹配)的更多相关文章

  1. hdu 2896 AC自动机

    // hdu 2896 AC自动机 // // 题目大意: // // 给你n个短串,然后给你q串长字符串,要求每个长字符串中 // 是否出现短串,出现的短串各是什么 // // 解题思路: // / ...

  2. hdu 3065 AC自动机

    // hdu 3065 AC自动机 // // 题目大意: // // 给你n个短串,然后给你一个长串,问:各个短串在长串中,出现了多少次 // // 解题思路: // // AC自动机,插入,构建, ...

  3. hdu 5880 AC自动机

    Family View Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  4. hdu 2296 aC自动机+dp(得到价值最大的字符串)

    Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  5. hdu 2825 aC自动机+状压dp

    Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  6. hdu 3065 AC自动机(各子串出现的次数)

    病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  7. HDU - 2222,HDU - 2896,HDU - 3065,ZOJ - 3430 AC自动机求文本串和模式串信息(模板题)

    最近正在学AC自动机,按照惯例需要刷一套kuangbin的AC自动机专题巩固 在网上看过很多模板,感觉kuangbin大神的模板最为简洁,于是就选择了用kuangbin大神的模板. AC自动机其实就是 ...

  8. HDU:2222-Keywords Search(AC自动机模板,匹配模拟)

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

  9. HDU 5384 AC自动机

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5384 题意:给n个母串,给m个匹配串,求每个母串依次和匹配串匹配,能得到的数目和. 分析:之前并不知道AC ...

随机推荐

  1. python获取指定星期的日期

  2. JQ JSON数据类型

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. Delphi之DLL知识学习1---什么是DLL

    DLL(动态链接库)是程序模块,它包括代码.数据或资源,能够被其他的Windows 应用程序共享.DLL的主要特点之一是应用程序可以在运行时调入代码执行,而不是在编译时链接代码,因此,多个应用程序可以 ...

  4. mac liteIDE调试配置

    http://studygolang.com/articles/1636 brew install https://raw.github.com/Homebrew/homebrew-dupes/mas ...

  5. bootstrap 练习

    bookList.html <!DOCTYPE html> <html lang="zh-cn"> <head> <!-- 父路径 --& ...

  6. js判断当前的访问是手机/电脑

    <script type="text/javascript"> var commonURL = 'http://www.xxx.com/'; function mobi ...

  7. PHP数组合并的常见问题

    一维数组的合并 <?php $arr1=array("a","b","c"); $arr2=array("c",& ...

  8. windows7下安装php的imagick和imagemagick扩展教程

    这篇文章主要介绍了windows7下安装php的imagick和imagemagick扩展教程,同样也适应XP操作系统,Win8下就没测试过了,需要的朋友可以参考下 最近的PHP项目中,需要用到切图和 ...

  9. C# NamePipe使用小结

    最近在一次项目中使用到了C#中命名管道,所以在此写下一篇小结备忘. 为什么要使用命名管道呢?为了实现两个程序之间的数据交换.假设下面一个场景.在同一台PC上,程序A与程序B需要进行数据通信,此时我们就 ...

  10. 协处理器,王明学learn

    协处理器 协处理器用于执行特定的处理任务,如:数学协处理器可以控制数字处理,以减轻处理器的负担.ARM可支持多达16个协处理器,其中CP15是最重要的一个. CP15提供16组寄存器 通过提供的16组 ...