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 ...
随机推荐
- GTD
这两天坚持GTD,四分象限法管理时间,感觉学习专注度明显提升,一直没完成的马士兵JAVA基础整到第八章了,继续保持,1.13号前争取11章全整完. 3个点支撑起你的职业发展:技术,管理(管理自己.管理 ...
- iOS开发笔记之Runtime实用总结
前言 runtime的资料网上有很多了,部分有些晦涩难懂,我通过自己的学习方法总结一遍,主要讲一些常用的方法功能,以实用为主,我觉得用到印象才是最深刻的.另外runtime的知识还有很多,想要了解更多 ...
- maxiang.io css
/**设置你自己的CSS.例如:h1 { border-bottom: 1px solid #ccc; line-height:1.6;}body { background:#FDFFD0} **/p ...
- git 实用操作
查看某文件的某些行的变化历史: $ git log --pretty=short -u -L 2003,2005:Executor.cpp http://stackoverflow.com/quest ...
- 【转】mysql存储引擎
http://www.cnblogs.com/kevingrace/p/5685355.html Mysql有两种存储引擎:InnoDB与Myisam,下表是两种引擎的简单对比 MyISAM In ...
- linux替换文件指定字符串前面的内容
sed 's/.*user_id/user_id/' wechat_log2 > target_log
- Java使用正则表达式取网页中的一段内容(以取Js方法为例)
关于正则表达式: 表1.常用的元字符 代码 说明 . 匹配除换行符以外的任意字符 \w 匹配字母或数字或下划线或汉字 \s 匹配任意的空白符 \d 匹配数字 \b 匹配单词的开始或结束 ^ 匹配字符串 ...
- Lua 单例类
function SingleTon:new() local store = nil return function(self) if store then return store end loca ...
- Visual Studio2008环境下查找C#中方法的“查看所有引用”
在Visual Studio开发环境下,想必F12我们都很熟悉了,有没有用过“查看所有引用”呢? 尤其是在一个解决方案中,包含了很多项目,彼此相互的调用是很常见的,例如三层架构, BLL调用DAL,D ...
- 20151210study
-----------------------------------------------------The election officials were not neutral.选举官员并不是 ...