【HDU 2222】Keywords Search AC自动机模板题
参考iwtwiioi的模板写出来的。上午gty讲的并没有听懂,只好自己慢慢对着模板理解。
在HDU上为什么相同的程序提交有时T有时A!!!
奉上sth神犇的模板(不是这道题):
var
ch:char;
q,g,num:array [0..500001] of longint;
st:string;
son:array [0..500001,'a'..'z'] of longint;
ts:array [0..1000001] of char;
l,s,t,n,i,j,m,k,ans,head,tail:longint;
begin
readln(t);
while t<>0 do
begin
for ch:='a' to 'z' do begin son[0,ch]:=1; son[1,ch]:=0; end;
dec(t);
s:=1;
g[1]:=0;
fillchar(num,sizeof(num),0);
readln(n);
for i:=1 to n do begin
readln(st);
m:=length(st);
l:=1;
for j:=1 to m do
if son[l,st[j]]<>0 then l:=son[l,st[j]]
else begin
inc(s);
g[s]:=0;
son[l,st[j]]:=s;
l:=s;
for ch:='a' to 'z' do son[s,ch]:=0;
end;
inc(num[l]);
end;
s:=1;
head:=0;
tail:=1;
q[1]:=1;
n:=0;
while head<tail do
begin
inc(head);
k:=q[head];
for ch:='a' to 'z' do
if son[k,ch]=0 then son[k,ch]:=son[g[k],ch]
else begin g[son[k,ch]]:=son[g[k],ch];
inc(tail);
q[tail]:=son[k,ch];
end;
end;
然后是我的ACcode:
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i));
using namespace std;
char a[10010],s[1000010];
int ch[500010][30],fail[500010],w[500010],ans,cnt;
inline void ins(char *b){
int u=0,v,len=strlen(b);
for2(i,0,len){
v=b[i]-'a';
if (!ch[u][v])ch[u][v]=cnt++;
u=ch[u][v];
}w[u]++;
}
inline void bfs(){
queue<int>q;
q.push(0);
int u,p;
while (!q.empty()){
u=q.front();q.pop();
for2(i,0,26) if (ch[u][i]){
q.push(ch[u][i]);
if (!u) continue;
p=fail[u];
while (p&&!ch[p][i])p=fail[p];
fail[ch[u][i]]=ch[p][i];
}
}
}
inline void ac(char *b){
int u=0,v,len=strlen(b),t;
for2(i,0,len){
v=b[i]-'a';
while (u&&!ch[u][v])u=fail[u];
u=ch[u][v];
t=u;
while (t){
ans+=w[t];
w[t]=0;
t=fail[t];
}
}
}
inline void init(){CC(fail,0);CC(ch,0);CC(w,0);ans=0;cnt=1;}
int main(){
int T;scanf("%d",&T);
while (T--){
init(); int n;
scanf("%d",&n);
for1(i,1,n){
scanf("%s",a);
ins(a);
}bfs();
scanf("%s",s);
ac(s);
printf("%d\n",ans);
}return 0;
}
【HDU 2222】Keywords Search AC自动机模板题的更多相关文章
- HDU 2222 Keywords Search(AC自动机模板题)
学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
- hdu 2222 Keywords Search ac自动机入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...
- hdu 2222 Keywords Search——AC自动机
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...
- HDU 2222 Keywords Search (AC自动机)
题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
- POJ2222 Keywords Search AC自动机模板
http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...
- Keywords Search(AC自动机模板)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
随机推荐
- Ubuntu 14.04 LTS Server 无法挂载光盘 启动initramfs等问题
今天需要在戴尔R410服务器上装64位的Linux,师兄给了个14.04的server 64位镜像.一开始打算用U盘安装,用软碟通烧写镜像之后,在服务器端设置从U盘启动,但是安装到一半出现了光盘无法挂 ...
- 【软件】图文解释XCode常用快捷键的使用
一.关于运行调试 1.运行,停止,都在工具栏的Product里. Command + R 运行. Command + . 停止 2.F6单步调试.F7跳入,F8继续, 和Eclipse,VS类似 ...
- OpenSSH后门获取root密码及防范
OpenSSH后门获取root密码及防范 相对于Windows操作系统,Linux操作系统的密码较难获取.而很多Linux服务器都配置了Openssh服务,在获取root权限的情况下,可以通过修改或者 ...
- Android配置----Eclipse+BlueStacks调试Android应用
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...
- ExtJS要利用观察者模式 去实现自定义的事件
// 要利用观察者模式 去实现自定义的事件 //1:由于浏览器他自己能定义内置的事件(click/blur...) // 我们也应该有一个类似于浏览器这样的类,这个类 自己去内部定义一些事件(自定义事 ...
- Yii提供的Htmler助手checkboxList可自定义Checkbox输出格式
foreach($catetags as $cate){ echo Html::checkboxList('category_id','',$cate,['item'=>'customCheck ...
- Gitblit-Git版本服务器环境部署
Gitblit介绍 Gitblit 是一个纯 Java 库用来管理.查看和处理 Git 资料库.相当于 Git 的 Java 管理工具,支持linux系统. Git是分布式版本控制系统,它强调速度.数 ...
- 012医疗项目-模块一:统一异常处理器的设计思路及其实现(涉及到了Springmvc的异常处理流程)
我们上一篇文章是建立了一个自定义的异常类,来代替了原始的Exception类.在Serice层抛出异常,然后要在Action层捕获这个异常,这样的话在每个Action中都要有try{}catch{}代 ...
- XCode的 Stack Trace,调试时抛出异常,定位到某一行代码
在Xcode调试程序的时候,总是会出现不知道错误在什么地方的问题,很是捉急,现在又一个办法,可以具体定位到错误行的代码,试一下吧?超级好用 操作很简单: 1.在XCode界面中按cmd + 6快捷键, ...
- 一些正则验证-JS
Validation = { textCount: function(field, counter, maxLimit) { var message = $(field).val(); if ($(f ...