HDU 2222 & ac自动机模板
题意:
求n个模板串在匹配串中出现了几个.
SOL:
反正就是模板啦...似乎比KMP都简单----这么说似乎有点不道德...毕竟先看的KMP而他们并没有什么不同...
貌似自己的理解和他们画的图还是有些出入......不虚慢慢看...
然后就是特殊一点的一个last数组,可以比较迅速地找到包含的子串.
这个题目会出现相同的模板...没看懂老人家开map的意图,第一遍用vector打,然后这种统计数量不是直接开个num记录数量就好了吗...
然而为毛我的num比开vector还慢呢...
/*==========================================================================
# Last modified: 2016-03-02 19:00
# Filename: a.cpp
# Description:
==========================================================================*/
#define me AcrossTheSky
#include <cstdio>
#include <cmath>
#include <ctime>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> #include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector> #define lowbit(x) (x)&(-x)
#define FOR(i,a,b) for((i)=(a);(i)<=(b);(i)++)
#define FORP(i,a,b) for(int i=(a);i<=(b);i++)
#define FORM(i,a,b) for(int i=(a);i>=(b);i--)
#define ls(a,b) (((a)+(b)) << 1)
#define rs(a,b) (((a)+(b)) >> 1)
#define getlc(a) ch[(a)][0]
#define getrc(a) ch[(a)][1] #define maxn 1000200
#define maxm 100000
#define maxc 30
#define pi 3.1415926535898
#define _e 2.718281828459
#define INF 1070000000
using namespace std;
typedef long long ll;
typedef unsigned long long ull; template<class T> inline
void read(T& num) {
bool start=false,neg=false;
char c;
num=0;
while((c=getchar())!=EOF) {
if(c=='-') start=neg=true;
else if(c>='0' && c<='9') {
start=true;
num=num*10+c-'0';
} else if(start) break;
}
if(neg) num=-num;
}
/*==================split line==================*/
char temp[maxn],s[60];
int ch[500005][maxc],f[500005],last[500005];
vector<int> val[500005];
int sz,ans;
void reset(){
sz=1; ans=0;
memset(ch,0,sizeof(ch)); memset(last,0,sizeof(last));
memset(val,0,sizeof(val)); memset(f,0,sizeof(f));
}
int idx(char c){return c-'a';}
void insert(char *s,int v){
int u=0,len=strlen(s);
FORP(i,0,len-1){
int c=idx(s[i]);
if (!ch[u][c]) {
while(val[sz].size()) val[sz].pop_back();
ch[u][c]=sz++;
}
u=ch[u][c];
}
val[u].push_back(v);
}
void add(int j){
while(val[j].size()){
ans+=val[j].size();
while (val[j].size()) val[j].pop_back();
j=last[j];
}
}
void find(char *T){
int n=strlen(T);
int j=0;
FORP(i,0,n-1){
int c=idx(T[i]);
j=ch[j][c];
if (val[j].size()) add(j);
else if(last[j]) add(last[j]);
}
}
void getfail(){
queue<int>q;
f[0]=0;
FORP(c,0,maxc-1){
int u=ch[0][c];
if (u) {
f[u]=0; last[u]=0;
q.push(u);
}
}
while (!q.empty()){
int r=q.front(); q.pop();
FORP(c,0,maxc-1){
int u=ch[r][c];
if (!u) {ch[r][c]=ch[f[r]][c]; continue;}
q.push(u);
int v=f[r];
while (v && !ch[v][c]) v=f[v];
f[u]=ch[v][c];
last[u]=val[f[u]].size()?f[u]:last[f[u]];
}
}
}
int main(){
int cas; read(cas);
while (cas--){
reset();
int n; read(n);
FORP(i,1,n){
scanf("%s",s);
insert(s,i);
}
scanf("%s",temp);
getfail();
find(temp);
printf("%d\n",ans);
}
}
HDU 2222 & ac自动机模板的更多相关文章
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
- HDU 2222 (AC自动机)
HDU 2222 Keywords search Problem : 给若干个模式串,询问目标串中出现了多少个模式串. Solution : 复习了一下AC自动机.需要注意AC自动机中的fail,和n ...
- HDU 2222 ----AC自动机
Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...
- HDU 2222 AC自动机 裸题
题意: 问母串中出现多少个模式串 注意ac自动机的节点总数 #include <stdio.h> #include <string.h> #include <queue& ...
- HDU 2222 AC自动机模版题
所学的AC自动机都源于斌哥和昀神的想法. 题意:求目标串中出现了几个模式串. 使用一个int型的end数组记录,查询一次. #include <cstdio> #include <c ...
- hdu 2222(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu 2222 ac自动机更新模板 for onSite contest
http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 #include <cstdio> #include <cstdlib> ...
随机推荐
- Yslow网站性能优化工具
Yslow是一款网站性能优化的插件:
- .net学习笔记----WebConfig常用配置节点介绍
一.配置文件入门 .Net提供了一种保存项目配置信息的办法,就是利用配置文件,配置文件的后缀一般是.config.在WinForm程序中配置文件一般是App.config.在Asp.net中一般默认是 ...
- 《CLR via C#》读书笔记(6)类型和成员基础
6.1 类型的各种成员 在一个类型中,可以定义0个或者多个以下种类的成员: 常量 常量是在编译时设置其值并且永远不能更改其值的字段.使用常量可以为特殊值提供有意义的名称以代替数字文本,以使代码变得更容 ...
- Jquery.Datatables td宽度太长的情况下,自动换行
在 td 里面 加上 style="word-wrap:break-word;" 自动换行就好了,如果不想换行,可以将超出内容设为隐藏, overflow:hidden; whit ...
- Redis简介、与memcached比较、存储方式、应用场景、生产经验教训、安全设置、key的建议、安装和常用数据类型介绍、ServiceStack.Redis使用(1)
1.NOSQL简介 nosql的产生并不是要彻底的代替关系型数据库,而是作为传统关系型数据库的一个补充. Facebook和360使用Cassandra来存储海量社交数据 Twitter在其url抓取 ...
- 一个简单的Promise 实现
用了这么长时间的promise,也看了很多关于promise 的文章博客,对promise 算是些了解.但是要更深的理解promise,最好的办法还是自己实现一个. 我大概清楚promise 是对异步 ...
- php重修
阅读顺序: http://www.laruence.com/2008/08/11/147.html 深入浅出php http://www.laruence.com/2008/06/18/221.ht ...
- HDU4495 Rectangle
求组成的等腰三角形面积最大值. 对此题的总结:暴力出奇迹 组成的三角形放置方式一共只有4种,用ans表示目前已知的最长三角形的边长,从上到下,从左到右枚举顶点,再枚举边长,一个重要剪枝是枚举边长l时先 ...
- 简单的redis 性能测试
C:\Users\luhan.qian\Desktop\Tools\redis C:\Users\luhan.qian\Desktop\Tools\redis $ redis-benchmark.ex ...
- Debian下安装vim
问题描述:安装完系统以后,刚要打算开始写程序,发现,vim还没有装,用su -切换到root后 直接运行apt-get install vim,提示插入disc源,然后回车,陷入无法解决的状态. 上网 ...