hdu 3065 AC自动机
// hdu 3065 AC自动机
//
// 题目大意:
//
// 给你n个短串,然后给你一个长串,问:各个短串在长串中,出现了多少次
//
// 解题思路:
//
// AC自动机,插入,构建,查询就OK啦
//
// 感悟:
//
// 这道题真的是1A的哟~~~很开心~~~尽管是个裸地,继续加油哟~~~FIGHTING #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std; const int MAX_NODE = ;
const int SIGMA = ; struct Aho_Corasick{ int ch[MAX_NODE][SIGMA];
int f[MAX_NODE];
int val[MAX_NODE];
int last[MAX_NODE];
int cnt[];
int sz; void init(){
sz = ;
memset(ch[],,sizeof(ch[]));
memset(cnt,,sizeof(cnt));
val[] = ;
} void insert(char *s, int v){
int u = ;
int n = strlen(s);
for (int i=;i<n;i++){
int c = s[i];
if (!ch[u][c]){
memset(ch[sz],,sizeof(ch[sz]));
val[sz] = ;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = v;
} void getfail(){
queue<int> que;
for (int c = ;c < SIGMA; c++){
int u = ch[][c];
if(u){
que.push(u);
f[u] = ;
last[u] = ;
}
} while(!que.empty()){
int r = que.front();
que.pop(); for (int c = ; c < SIGMA;c++){
int u = ch[r][c]; if (!u)
continue; que.push(u); int v = f[r]; while( v && !ch[v][c])
v = f[v]; f[u] = ch[v][c]; last[u] = val[f[u]] ? f[u] : last[f[u]];
}
}
} void get_cnt(int u){
if (u){
cnt[val[u]]++;
get_cnt(last[u]);
}
} void query(char *s){
int u = ; int n = strlen(s); for(int i=;i<n;i++){
int c = s[i]; while(u && !ch[u][c])
u = f[u]; u = ch[u][c]; if (val[u]){
get_cnt(u);
}else if (last[u]){
get_cnt(last[u]);
}
}
} }ac; int n; char p[][]; char str[]; void input(){
ac.init();
char s[];
for (int i=;i<=n;i++){
scanf("%s",s);
memcpy(p[i],s,sizeof(s));
ac.insert(s,i);
}
ac.getfail(); scanf("%s",str); ac.query(str);
for (int i = ;i<=n;i++){
if (ac.cnt[i]){
printf("%s: %d\n",p[i],ac.cnt[i]);
}
}
} int main(){
//freopen("1.txt","r",stdin);
while(scanf("%d",&n)!=EOF){
input();
}
return ;
}
hdu 3065 AC自动机的更多相关文章
- hdu 3065 AC自动机(各子串出现的次数)
病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- hdu 3065 AC自动机 标记数组不清零
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目里面要我们计算每种单词出现的次数,重叠的也要计算,那么我们在查找的时候不要把标记单词结尾的 ...
- 病毒侵袭持续中 HDU - 3065 AC自动机
小t非常感谢大家帮忙解决了他的上一个问题.然而病毒侵袭持续中.在小t的不懈努力下,他发现了网路中的"万恶之源".这是一个庞大的病毒网站,他有着好多好多的病毒,但是这个网站包含的病毒 ...
- HDU 3065 AC自动机 裸题
中文题题意不再赘述 注意 失配数组 f 初始化一步到位 #include <stdio.h> #include <string.h> #include <queue&g ...
- hdu 3065 AC自动机模版题
题意:输出每个模式串出现的次数,查询的时候呢使用一个数组进行记录就好. 同上题一样的关键点,其他没什么难度了. #include <cstdio> #include <cstring ...
- hdu 2896 AC自动机
// hdu 2896 AC自动机 // // 题目大意: // // 给你n个短串,然后给你q串长字符串,要求每个长字符串中 // 是否出现短串,出现的短串各是什么 // // 解题思路: // / ...
- hdu 5880 AC自动机
Family View Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- hdu 2296 aC自动机+dp(得到价值最大的字符串)
Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
随机推荐
- 在oracle里写各种语句得心应手,但是在mybatis.xml文件里呢?
这个问题我让我搞了大半天,实在气人,话不多说,直接上代码 <select id="*" resultMap="Blog" parameterType=&q ...
- 关于arduino清空串口缓存(转)
在arduino1.0之前的版本 Serial.flush()的作用是是清空串口缓存( dropping received incoming data).但在1.0之后的版本 Serial.flush ...
- Pow 算法
#include <iostream> using namespace std; template<class T, class Int> T Pow(T x, Int n) ...
- UpdatePanel 中 导出Excel按钮
UpdatePanel 中 导出Excel按钮 要加 Triggers </ContentTemplate> <Triggers> <asp:PostBackTrigge ...
- 使用 Date 和 SimpleDateFormat 类表示时间、Calendar类和Math类
一. Date 和 SimpleDateFormat类表示时间 在程序开发中,经常需要处理日期和时间的相关数据,此时我们可以使用 java.util 包中的 Date 类.这个类最主要的作用就是获取当 ...
- Coursera Machine Learning 作业答案脚本 分享在github上
Github地址:https://github.com/edward0130/Coursera-ML
- 使用SVN同步资源后图标样式的详细解读
项目视图 The Package Explorer view - 已忽略版本控制的文件.可以通过Window → Preferences → Team → Ignored Resources.来忽 ...
- JavaWeb 学习001-登录页面
首先实现一个web应用的登录页面 1.遇到的问题: Servlet中 post 或者 get 方式 不能提交? 就是提交后,控制台没有反应,而且浏览器显示如图: 这样应该是不能进行页面的跳转,对这个, ...
- js正则获取图片的src属性及正则分割一个字符串
try{ var str='<P>xczxzxzxcxcxc<IMG src="http://file.ynet.com/2/1612/12/12119048.jp ...
- js ShowDialogModal 关闭子页面并刷新父页面,保留查询条件
不知道大家有没有碰到类似的问题,当时的你是什么思路来处理这个问题呢?是url,session,cookie,还是…… 今天笔者就遇到了这个问题,当时的想法如:url,session,cookie都尝试 ...