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. SAP连接HANA数据库

    既然都用HANA了,为什么还要在SAP端,连接HANA数据库,做数据库处理..... 因为HANA数据库,没个用户在STADIO上建的数据库表...只能这个用户使用,而做Universe 设计的时候, ...

  2. Axis2 webservice入门--写个简单的webservice

    上一篇介绍了webservice开发前的准备.下面开始写webservice.如果不了解axis2请看上一篇,如果是新手:建议一边看一边写代码,自己动手完成这个过程. 一.新建一个web项目 二.新建 ...

  3. ASP.NET MVC统一异常处理

    前言: 今早看了篇文章:求知成瘾,却无作品 的思考:很有感触,发现原来自己也是这样,对每样东西都抱有很大的兴趣或者希望自己去学,一年后发现原来自己什么都是皮毛什么都不精!最终发现真正的大牛都是在某一个 ...

  4. HDU 3074 Multiply game(线段树)

    单点更新,更新时先除去 原来的数,因为有去摸,可以用乘上逆元代替. //================================================================ ...

  5. Web体系=资源+URI+表示

    概述 Web有三个核心概念:资源(Resource).URI(UniformResource Identifer,统一资源标识符).表示(Representation).一个资源由一个URI进行标识. ...

  6. SrcollView分页加载数据(第二种方法 自定义listView)

    package com.baidu.ms; import android.content.Context;import android.util.AttributeSet;import android ...

  7. InterruptedException 线程异常

    InterruptedException 这个异常一般发生在线程中,当一个正在执行的线程被中断时就会出现这个异常-! 简单的说就是:假如有两个线程,第一个线程正在运行,第二个没有运行,这时第二个线程启 ...

  8. SharePoint表单和工作流 - Nintex篇(四)

    博客地址 http://blog.csdn.net/foxdave 接上篇点击打开链接 "Manage workflow constants" 管理工作流常量.这里可以管理工作流中 ...

  9. Oracle GoldenGate: 使用宏

         OGG宏与C语言中的宏一样,提供了函数封装的功能,即可以将一些配置参数整理为一个宏,然后在多个参数文件中共用,针对环境复杂或多个复制点的情况尤其有用.下面我们将介绍如何创建一个宏的库,以及在 ...

  10. Struts2 和 spring mvc的 迭代标签常用属性对比

    <s:iterator value="#users" var="u" status="st"> <c:forEach  i ...