AC自动机模板题。

被卡内存了 死活A不掉。。

AC自动机参考教程:

  http://www.cppblog.com/menjitianya/archive/2014/07/10/207604.html

 const maxn=;
type arr=record
next:array[..] of longint;
fail,cnt:longint;
end;
var cas:longint;
s:array[..maxn] of string;
T:array[..] of arr;
id:array['a'..'z'] of longint;
rt,tmp:longint;
b:array[..] of longint;
procedure ins(str:string);
var now,i:longint;
begin
now:=rt;
for i:= to length(str) do
begin
if T[now].next[id[str[i]]]= then
begin
inc(tmp);
T[now].next[id[str[i]]]:=tmp;
end;
now:=T[now].next[id[str[i]]];
end;
inc(T[now].cnt);
end;
procedure build;
var l,r,u,v,i:longint;
begin
fillchar(b,sizeof(b),);
r:=;
for i:= to do
if T[rt].next[i]<> then
begin
T[T[rt].next[i]].fail:=rt;
inc(r);
b[r]:=T[rt].next[i];
end;
l:=;
while l<=r do
begin
u:=b[l];
for i:= to do
begin
v:=T[u].next[i];
if v= then
T[u].next[i]:=T[T[u].fail].next[i]
else
begin
T[v].fail:=T[T[u].fail].next[i];
inc(r);
b[r]:=v;
end;
end;
inc(l);
end;
end;
function query(st:ansistring):longint;
var i,ans,now,j:longint;
begin
ans:=; now:=rt;
for i:= to length(st) do
begin
now:=T[now].next[id[st[i]]];
j:=now;
while T[j].cnt> do
begin
ans:=ans+T[j].cnt;
T[j].cnt:=;
j:=T[j].fail;
end;
ans:=ans+T[T[j].fail].cnt;
T[T[j].fail].cnt:=;
end;
exit(ans);
end;
procedure main;
var i,n:longint;
st:ansistring;
begin
fillchar(T,sizeof(T),);
for i:= to do id[chr(+i)]:=i;
readln(n);
for i:= to n do readln(s[i]);
rt:=; tmp:=;
for i:= to n do ins(s[i]);
build;
readln(st);
writeln(query(st));
end;
begin
readln(cas);
while cas> do begin dec(cas); main; end;
end.

HDU2222 (AC自动机)的更多相关文章

  1. hdu2222 ac自动机入门

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. HDU2222(AC自动机入门题)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  3. [hdu2222] [AC自动机模板] Keywords Search [AC自动机]

    AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! #include <iostream> #include <algorithm> #i ...

  4. Keywords Search HDU2222 AC自动机模板题

    ac自动机说起来很复杂,其实和kmp是一样的思路,都是寻找相同前后缀,减少跳的次数.只要理解了kmp是怎么求next数组的,ac自动机bfs甚至比knp还好写. 这里大致说一下kmp求next数组的方 ...

  5. [hdu2222]ac自动机(模板)

    题意:一个文本串+多个模板串的匹配问题 思路:裸的ac自动机. #pragma comment(linker, "/STACK:10240000,10240000") #inclu ...

  6. hdu2222 AC自动机

    字典树也可以做. #include<stdio.h> #include<string.h> #include<stdlib.h> #define maxn 1000 ...

  7. hdu2222(ac自动机模板)

    #include<iostream> #include<cmath> #include<cstdio> #include<cstring> #inclu ...

  8. 【HDU2222】Keywords Search AC自动机

    [HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...

  9. 【HDU2222】Keywords Search(AC自动机)

    Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...

随机推荐

  1. IOS开发设计思路

    我在做 iOS 开发的时候,发现自己在写程序的时候,常常处于两种状态的切换,我把这两种状态称为软件开发的上帝模式与农民模式.我先给大家介绍一下这两种模式的特点. 上帝模式 处于上帝模式时,我需要构思整 ...

  2. 初始maven

    Apache Maven 是一个软件项目管理和综合工具.基于项目对象模型 (POM) 的概念,Maven 可以管理一个项目的生成. 报告和文档从一块中央的信息.在JavaEE中,我们可以使用Maven ...

  3. for循环练习——7月23日

    练习一:输入一个整数,求从1到这个数的累加和 //练习1:输入一个整数,计算从1加到这个数的结果 Console.Write("请输入一个正整数:"); int a = int.P ...

  4. 怎样打造一个分布式数据库——rocksDB, raft, mvcc,本质上是为了解决跨数据中心的复制

    摘自:http://www.infoq.com/cn/articles/how-to-build-a-distributed-database?utm_campaign=rightbar_v2& ...

  5. mongodb 和 mysql 的对照

    In addition to the charts that follow, you might want to consider the Frequently Asked Questions sec ...

  6. 又一个提示框思密达,腾讯UED

    demo <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf- ...

  7. 【vmware vcp 5.1】安装及配置及笔记散记

    ESXi的几个命令技巧: ------------------------------------------------- alt-f1: 进入console alt-f2: 返回DCUI alt- ...

  8. AlarmManager手机闹钟简介

    1.void set(int type , long triggerAtTime , PendingIntent operation ) : 设置在 triggerAtTime时间启动由operati ...

  9. oracle查询语句大全

    1.查处oracle数据库的字符编码格式 select * from v$nls_parameters t where t.PARAMETER='NLS_CHARACTERSET'; 如果查出的是ZH ...

  10. js基础之弹性运动(四)

    一.滑动菜单.图片 var iSpeed=0;var left=0;function startMove(obj,iTarg){ clearInterval(obj.timer);//记得先关定时器 ...