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. java 多线程——一个定时调度的例子

    java 多线程 目录: Java 多线程——基础知识 Java 多线程 —— synchronized关键字 java 多线程——一个定时调度的例子 java 多线程——quartz 定时调度的例子 ...

  2. 前端面试题之nina分享

    HTML相关 1.<!DOCTYPE>标签的定义与用法. <!DOCTYPE>的定义: <!DOCTYPE>声明位于文档中的最前面的位置,处于<html> ...

  3. HDU 1688 Sightseeing

    题目链接:Sightseeing 题意:求最短路和比最短路长度+1的所有路径条数. 附代码:用数组记录最短和次短路径的长度和条数,一次更新,直到没有边可以更新. #include <stdio. ...

  4. CCF 2016-12 送货

    问题描述 试题编号: 201512-4 试题名称: 送货 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 为了增加公司收入,F公司新开设了物流业务.由于F公司在业界的良好口碑, ...

  5. if语句使用

    package yuan; public class Yuan { public static void main(String[] args) { int a = 1; int b = 8; int ...

  6. HDU 4046 Panda

    线段树单点更新,要注意两段合并多出的答案的计算即可 //======================================================================== ...

  7. javascript 错误处理

    try{ var date=new Date(); date.test();//调用date的未定义的test方法; document.wrire("try块执行结束<br>&q ...

  8. poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

    A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...

  9. java获取页面编码

    文章出自:http://babyjoycry.javaeye.com/blog/587527 在此感谢原作者...\(^o^)/~   最近研究抓取网页内容,发现要获取页面的编码格式,Java没有现成 ...

  10. android 禁止viewPager 滑动

    public class ContainerViewPager extends MyViewPager { public ContainerViewPager(Context context, Att ...