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. content.boundingRectWithSize计算出来的高度不准

      计算出来的高度会少一行的高度,最后一行会显示不全.减掉padding会解决这个问题.   let padding = self.reviewText.textContainer.lineFragm ...

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

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

  3. BZOJ3696 化合物

    我们可以树形dp... 令f[p][d]表示以p为根的子树,与p距离为d的结点数 然后我们计算答案: 一种是从某个节点q到根p的方案,对和为d的贡献是1 另一种是p的一个子树中的节点x到另一个子树中的 ...

  4. linux copy

    cp -ri 131115-6/* /web/www/attachment/  把13这个文件夹下面所有的文件和文件夹复制到 /web/www/attachment cp -a  a  /web/ww ...

  5. The APR based Apache Tomcat Native library 异常解决办法

    tomat在linux服务器上启动报The APR based Apache Tomcat Native library which allows optimal performance in pro ...

  6. php 采集类snoopy http://www.jb51.net/article/27568.htm | cURL、file_get_contents、snoopy.class.php 优缺点

    Snoopy是一个php类,用来模拟浏览器的功能,可以获取网页内容,发送表单. Snoopy的特点: 1.抓取网页的内容 fetch 2.抓取网页的文本内容 (去除HTML标签) fetchtext ...

  7. Query的选择器中的通配符[id^='code']或[name^='code']

        1.选择器 (1)通配符: $("input[id^='code']");//id属性以code开始的所有input标签 $("input[id$='code'] ...

  8. Jquery操作复选框(CheckBox)的取值赋值实现代码

    赋值 复选框 CheckBox 遍历 取值  1. 获取单个checkbox选中项(三种写法): $("input:checkbox:checked").val() 或者 $(&q ...

  9. bzoj 2440 简单莫比乌斯反演

    题目大意: 找第k个非平方数,平方数定义为一个数存在一个因子可以用某个数的平方来表示 这里首先需要考虑到二分才可以接下来做 二分去查找[1 , x]区间内非平方数的个数,后面就是简单的莫比乌斯反演了 ...

  10. BinaryWriter

    c#里的文件操作 fileInfo dir的一大堆属性不用说 地球人都知道(什么fileName,create() delete()) ,文件系统的概念很好理解的 文件读写也好理解(硬盘到内存 然后再 ...