HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065
题目大意:多个模式串,范围是大写字母。匹配串的字符范围是(0~127)。问匹配串中含有哪几种模式串,且每种模式串出现了多少次。
解题思路:
AC自动机模板题。模式串的范围是大写字母,但是匹配串的范围却是(0~127).
如果Trie 开到 128 加上不回收内存,就会MLE。
实际上开到26就行了,find的时候对于c<0||c>26,强制令pos=root出现失配,并开始下一个字符就行了。
cnt记录这个模式串的个数改为这个模式串的index。
find的时候,把找到的index对应的HASH++,进行统计。
注意每次find时,无须修改last->cnt,因为统计模式串的次数必须让模式串走多遍,修改了之后相当于只走了一遍。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include "cstdio"
#include "cstring"
#include "string"
#include "iostream"
#include "queue"
#include "vector"
#include "algorithm"
using namespace std;
#define maxn 26
int HASH[];
string code[];
struct Trie
{
Trie *next[maxn],*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,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)
{
Trie *pos=root,*last;
queue<status> Q;
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->cnt)
{
Q.push(status(last,last->cnt));
HASH[last->cnt]++;
//last->cnt=0; //修改last->cnt
last=last->fail;
}
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
ios::sync_with_stdio(false);
int n,m;
string tt;
while(cin>>n)
{
memset(HASH,,sizeof(HASH));
init();
for(int i=;i<=n;i++)
{
cin>>code[i];
Insert(code[i],i);
}
getfail();
cin>>tt;
find(tt);
for(int i=;i<=n;i++)
{
if(!HASH[i]) continue;
cout<<code[i]<<": "<<HASH[i]<<endl;
}
}
}
2872067 | neopenx | HDU | 3065 | Accepted | 16056 | 343 | C++ | 2469 |
2014-10-21 16:41:03
|
HDU 3065 (AC自动机模板题)的更多相关文章
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
- HDU 3065 AC自动机 裸题
中文题题意不再赘述 注意 失配数组 f 初始化一步到位 #include <stdio.h> #include <string.h> #include <queue&g ...
- hdu 3065 AC自动机模版题
题意:输出每个模式串出现的次数,查询的时候呢使用一个数组进行记录就好. 同上题一样的关键点,其他没什么难度了. #include <cstdio> #include <cstring ...
- HDU 2222(AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...
- hdu 3065 AC自动机
// hdu 3065 AC自动机 // // 题目大意: // // 给你n个短串,然后给你一个长串,问:各个短串在长串中,出现了多少次 // // 解题思路: // // AC自动机,插入,构建, ...
- hdu 3065 AC自动机(各子串出现的次数)
病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU3695(AC自动机模板题)
题意:给你n个字符串,再给你一个大的字符串A,问你着n个字符串在正的A和反的A里出现多少个? 其实就是AC自动机模板题啊( ╯□╰ ) 正着query一次再反着query一次就好了 /* gyt Li ...
- HDu-2896 病毒侵袭,AC自动机模板题!
病毒侵袭 模板题,不多说了.. 题意:n个不同的字符串分别代表病毒特征,给出m次查询,每次一个字符串(网址),求这个字符串中有几个病毒特征,分别从大到小输出编号,最后输出所有的带病毒网址个数.格式请看 ...
随机推荐
- :( Call to a member function Table() on a non-object 错误位置
:( Call to a member function Table() on a non-object 错误位置 $Model不是模板,是你自己先前链接数据库返回的对象...我的是改为$Form
- [官方说明] 为什么ES4要分成两阶段?
从ES4第一个版本发布到现在,已经有段时间了,绝大多数坛友都已经适应了ES4所带来的新封装模式,但仍有部分新人和坛友对ES4的两阶段模式带有不解或曲解.本帖将就ES4的两阶段意义做出解释说明,希望更多 ...
- BZOJ 1600
开始刷一些USACO月赛题了.. 这题简单递推就不说了. 然后我们发现暴力递推是$O(n^2)$的.看起来非常慢. 这道题拥有浓厚的数学色彩,因此我们可以从数学它的规律上找突破口. (于是暴力大法好, ...
- Largest Divisible Subset
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
- 2013 ACM/ICPC 长春网络赛F题
题意:两个人轮流说数字,第一个人可以说区间[1~k]中的一个,之后每次每人都可以说一个比前一个人所说数字大一点的数字,相邻两次数字只差在区间[1~k].谁先>=N,谁输.问最后是第一个人赢还是第 ...
- centos下编译安装mysql5.5/5.6
2013年11月16日 19:39:13 centos 6 mysql 5.5.28 我只说些我出错的地方: cmake后删除的方法是 xargs rm < install_manifest.t ...
- Office 2010启动时出现无法验证此应用程序的许可证的解决
Office 2010启动之后弹出一个窗口提示:Microsoft Office无法验证此应用程序的许可证.修复尝试失败或者已被用户取消.应用程序将立即关闭. 遇到这样的情况,原因是Office的系统 ...
- eclipse连接虚拟机
1.启动eclipse 2.打开 "Help/Install New Software..." 3.打开Add…… 4.输入Name: Genymobile Lo ...
- 字母排列_next_permutation_字典序函数_待解决
问题 B: 字母排列 时间限制: 1 Sec 内存限制: 64 MB提交: 19 解决: 5[提交][状态][讨论版] 题目描述 当给出一串字符时,我们逐个可以变换其字符,形成新的字符串.假如对这 ...
- HDU 1087 Super Jumping! Jumping! Jumping! 最大递增子序列
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...