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次查询,每次一个字符串(网址),求这个字符串中有几个病毒特征,分别从大到小输出编号,最后输出所有的带病毒网址个数.格式请看 ...
随机推荐
- Access数据库之偏移注入
/*转载请注明出处:珍惜少年时*/ 偏移注入主要是针对知道表,但是不知道字段的. 这里我已经知道了表明是:sys_admin 可以使用: select exists(selct * from sys_ ...
- mysql中limit与in不能同时使用的解决方式.
mysql中limit与in不能同时使用的解决方式. 分类: MySQL2011-10-31 13:53 1277人阅读 评论(0) 收藏 举报 mysqlsubquery MySQL5.1中子查询是 ...
- Ubuntu 更改文件夹权限及chmod详细用法
转自: http://blog.chinaunix.net/uid-21880738-id-1813031.html 对Document/目录下的所有子文件与子目录执行相同的权限变更: Documen ...
- 【云计算】开源装机自动化系统 CloudBoot OSInstall 介绍
"CloudBoot"(OSinstall) 发布了. 产品更新及特点如下: 新增虚拟化操作系统适配:支持主流操作系统:RedHat.CentOS.SUSE.Ubuntu.Wind ...
- String to Integer
Implement function atoi to convert a string to an integer. If no valid conversion could be performed ...
- php中常用魔术方法的举例
魔术方法是php面向对象特有的功能,并且有时候能实现意想不到的效果,包括前面提到的构造函数.析构函数.还有__clone函数,另外再简单的介绍几个: 1.__toSring和__invoke clas ...
- iOS国际化(Xcode5)
如何将你的app内的语言可以根据系统语言切换而切换呢?这是本篇所要解决的问题.废话先不说,上软硬件环境: 硬件:Macbook Pro 软件:Xcode 5.1 代码:https://github.c ...
- BSD学习(BSD系统的历史和目标)
UNIX系统的历史 unix系统的发展历程大概经历以下几个阶段: 贝尔实验室(Bell Laboratories)阶段,该实验室发明了UNIX 加州大学伯克利分校(University of Cali ...
- MFC RadioButton
添加一组RadioButton 多个radio button,IDC_RADIO1,IDC_RADIO2,IDC_RADIO3 ..将IDC_RADIO1的Group属性选择上,其他不要选Group属 ...
- struts标签--logic总结
1. logic:empty 该标签是用来判断是否为空的.如果为空,该标签体中嵌入的内容就会被处理.该标签用于以下情况: 1)当Java对象为null时: 2)当String对象为"&quo ...