const maxn=;
type data=record
sum,failed:longint;
son:array ['a'..'z'] of longint;
end; var tree:array [..maxn] of data;
que:array[..maxn] of longint;
boo:array[..maxn] of boolean;
s:array[..] of char;
test,n,len,tot,ans:longint; procedure PK(x:longint);
begin
fillchar(tree[x].son,sizeof(tree[x].son),);
tree[x].sum:=;
tree[x].failed:=-;
end; procedure init; procedure insert;
var p,i:longint;
begin
p:=;
for i:= to len do
begin
if tree[p].son[s[i]]= then
begin
inc(tot);
PK(tot);
tree[p].son[s[i]]:=tot;
p:=tot;
end else p:=tree[p].son[s[i]];
end;
inc(tree[p].sum);
end; var i:longint;
begin
fillchar(boo,sizeof(boo),false);
tot:=;
PK();
readln(n);
for i:= to n do
begin
len:=;
while not eoln do
begin
inc(len);
read(s[len]);
if not(s[len] in ['a'..'z']) then begin dec(len); break; end;
end;
readln;
insert;
end;
end; procedure main; procedure bfs;
var h,t,now,p,f:longint;
ch:char;
begin
h:=;t:=;
que[]:=;
while h<=t do
begin
now:=que[h];
for ch:='a' to 'z' do
begin
p:=tree[now].son[ch];
if p<> then
begin
f:=tree[now].failed;
while (f<>-) and (tree[f].son[ch]=) do f:=tree[f].failed;
if f=- then tree[p].failed:=
else tree[p].failed:=tree[f].son[ch];
inc(t);
que[t]:=p;
end;
end;
inc(h);
end;
end; procedure AC_Automaton;
var i,now,p,f:longint;
begin
i:=;
now:=;
while i<=len do
begin
p:=tree[now].son[s[i]];
if p<> then
begin
f:=p;
while (not boo[f]) and (f<>) do
begin
boo[f]:=true;
inc(ans,tree[f].sum);
f:=tree[f].failed;
end;
now:=p;
inc(i);
end else
begin
f:=now;
while (f<>-) and (tree[f].son[s[i]]=) do f:=tree[f].failed;
now:=f;
if now=- then
begin
now:=;
inc(i);
end;
end;
end;
end; begin
bfs;
len:=;
while not eoln do
begin
inc(len);
read(s[len]);
if not(s[len] in ['a'..'z']) then begin dec(len); break; end;
end;
readln;
ans:=;
AC_Automaton;
writeln(ans);
end; begin
readln(test);
while test> do
begin
init();
main();
dec(test);
end;
end.

AC自动机及trie图 pascal的更多相关文章

  1. POJ 2778 DNA Sequence ( AC自动机、Trie图、矩阵快速幂、DP )

    题意 : 给出一些病毒串,问你由ATGC构成的长度为 n 且不包含这些病毒串的个数有多少个 分析 : 这题搞了我真特么久啊,首先你需要知道的前置技能包括 AC自动机.构建Trie图.矩阵快速幂,其中矩 ...

  2. AC自动机——1 Trie树(字典树)介绍

    AC自动机——1 Trie树(字典树)介绍 2013年10月15日 23:56:45 阅读数:2375 之前,我们介绍了Kmp算法,其实,他就是一种单模式匹配.当要检查一篇文章中是否有某些敏感词,这其 ...

  3. AC自动机(trie图版)

    AC自动机是一个多模字符串匹配的自动机(网上说的),主要作用是在一个长串中同时进行多个字符串的匹配 基础芝士: trie树(字典树) 烤馍片kmp单模字符串匹配 如果不会的建议去网上学一下(本篇讲解略 ...

  4. AC自动机学习笔记-2(Trie图&&last优化)

    我是连月更都做不到的蒟蒻博主QwQ 考虑到我太菜了,考完noip就要退役了,所以我决定还是把博客的倒数第二篇博客给写了,也算是填了一个坑吧.(最后一篇?当然是悲怆のnoip退役记啦QAQ) 所以我们今 ...

  5. 小菜鸟 菜谈 KMP->字典树->AC自动机->trie 图 (改进与不改进)

    本文的主要宗旨是总结自己看了大佬们对AC自动机和trie 图 的一些理解与看法.(前沿:本人水平有限,总结有误,希望大佬们可以指出) KMP分割线--------------------------- ...

  6. 字符串 --- KMP Eentend-Kmp 自动机 trie图 trie树 后缀树 后缀数组

    涉及到字符串的问题,无外乎这样一些算法和数据结构:自动机 KMP算法 Extend-KMP 后缀树 后缀数组 trie树 trie图及其应用.当然这些都是比较高级的数据结构和算法,而这里面最常用和最熟 ...

  7. BZOJ 2754 [SCOI2012]喵星球上的点名 (AC自动机+map维护Trie树)

    题目大意:略 由于字符集大,要用map维护Trie树 并不能用AC自动机的Trie图优化,不然内存会炸 所以我用AC自动机暴跳fail水过的 显然根据喵星人建AC自动机是不行的,所以要根据问题建 然而 ...

  8. BZOJ2553 [BeiJing2011]禁忌 AC自动机 矩阵

    原文链接http://www.cnblogs.com/zhouzhendong/p/8196279.html 题目传送门 - BZOJ2553 题意概括 引用一下lych大佬的: 在字母只有前alph ...

  9. 专题训练之AC自动机

    推荐博客:http://www.cnblogs.com/kuangbin/p/3164106.html AC自动机小结 https://blog.csdn.net/creatorx/article/d ...

随机推荐

  1. Mina的ProtocolEncoderOutput之坑

    这个问题是之前测试上万个连接的pingpong发现的,调了很久,但当时急着做其他事,就没有详细记录下来,现在回想起来也有点记不清具体哪儿了. 表现是在测试上万个连接的pingpong时会出现服务器挂起 ...

  2. [HTML/CSS] ul元素居中处理

    CSS: <style type="text/css"> #nav_sub_page { text-align: center; width: 80%; } #nav_ ...

  3. D​e​p​l​o​y​m​e​n​t​ ​f​a​i​l​u​r​e​ ​o​n​ ​T​o​m​c​a​t​ ​6​.​x​.​ ​C​o​u​l​d​ ​n​o​t​ ​c​o​p​y​ ​a​l​l​ ​r​e​s​o​u​r​c​e​s​ ​t​o

    在myeclipse总部署项目,一直有问题,提示如下的错误,经过研究在网上需求帮助,解决方案如下: Deployment failure on Tomcat  6.x. Could not copy  ...

  4. ASP.NET MVC异步上传文件

    自己做的一个小dome.贴出来分享一下: 前端: <form id="formfile" method="post" enctype="mult ...

  5. 怎么解决xp系统不能安装NET Framework4.0?

    第一步: 如果是XP系统: 1.开始——运行——输入cmd——回车——在打开的窗口中输入net stop WuAuServ 2.开始——运行——输入%windir% 3.在打开的窗口中有个文件夹叫So ...

  6. VS2013 破解

    密钥 : BWG7X-J98B3-W34RT-33B3R-JVYW9

  7. Scala 高级编程练习

    定义自己的控制结构,使它语言内置的用法一样 package exp object Main { def enableCustomerIF = false; def main(args: Array[S ...

  8. Jupyter Notebook 27绝技——27 Jupyter Notebook tips, tricks and shortcuts

    转载自:https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/ Jupyter notebook, formerly ...

  9. How to create your own custom 404 error page and handle redirect in SharePoint 分类: Sharepoint 2015-07-08 00:22 4人阅读 评论(0) 收藏

    1. In your MOSS server, make a copy of %systemdrive%\Program Files\Common Files\Microsoft Shared\Web ...

  10. python 类以及单例模式

    python 也有面向对象的思想,则一切皆对象 python 中定义一个类: class student: count = 0         books = [] def __init__(self ...