HDU2222 (AC自动机)
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自动机)的更多相关文章
- hdu2222 ac自动机入门
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU2222(AC自动机入门题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- [hdu2222] [AC自动机模板] Keywords Search [AC自动机]
AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! #include <iostream> #include <algorithm> #i ...
- Keywords Search HDU2222 AC自动机模板题
ac自动机说起来很复杂,其实和kmp是一样的思路,都是寻找相同前后缀,减少跳的次数.只要理解了kmp是怎么求next数组的,ac自动机bfs甚至比knp还好写. 这里大致说一下kmp求next数组的方 ...
- [hdu2222]ac自动机(模板)
题意:一个文本串+多个模板串的匹配问题 思路:裸的ac自动机. #pragma comment(linker, "/STACK:10240000,10240000") #inclu ...
- hdu2222 AC自动机
字典树也可以做. #include<stdio.h> #include<string.h> #include<stdlib.h> #define maxn 1000 ...
- hdu2222(ac自动机模板)
#include<iostream> #include<cmath> #include<cstdio> #include<cstring> #inclu ...
- 【HDU2222】Keywords Search AC自动机
[HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- 【HDU2222】Keywords Search(AC自动机)
Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...
随机推荐
- 采购订单限价(包含阶梯价)ME_PROCESS_PO_CUST
为了能管控到钱财的每个环节,公司要求采购订单需要使用物料限价,当有报价低于先前的价格时,在采购订单保存时,更新最低限价 BADI:ME_PROCESS_PO_CUST process~item里做限价 ...
- 22. Generate Parentheses——本质:树,DFS求解可能的path
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 在Oracle中使用rank()over()排名的问题
排序: ---rank()over(order by 列名 排序)的结果是不连续的,如果有4个人,其中有3个是并列第1名,那么最后的排序结果结果如:1 1 1 4 select scoreid, st ...
- oracle建库及plsql建表空间的用法
所有程序—>ORACLE-JHEMR----------->配置和移植工具----->DataBase Configuration Assistant-------中间就需要改一个数 ...
- C#入门篇6-5:字符串操作 测试StringBuilder的运行效率
//测试StringBuilder的运行效率 public static void Fun2() { #region string string str = "我喜欢编程!"; / ...
- 在Excel中引用其他宏
在excel的使用过程中,会用到一些自定义函数,可以使用宏轻松的实现这些功能,问题是必须使用“启用宏的excel”,这样用户每次打开时都要启用宏. 现用以按背景色计划为例,解决以上问题: 1.新建一个 ...
- 使用Lucene.Net管理索引实现搜索
之前使用一直是没有问题的,只到今天发现删除的时候无法删除,增加的时候却一直在增加,导致搜索的时候可以搜出来很多相同的结果. 小猪决定趁今天这个机会好好的把这个问题给解决了. private void ...
- MySQL使用随笔
001 查看版本 mysql --version mysql > select version(); mysql > status; 002 新建MySQL用户.授权 insert int ...
- Protocol Buffers in HBase
For early Hbase developers, it is often a nightmare to understand how the different modules speak am ...
- 使用ASP.Net WebAPI构建REST服务(二)——路由
REST并没有像传统的RPC服务那样显式指定了服务器函数的访问路径,而是将URL根据一定的规则映射为服务函数入口,这个规则就称之为路由.Asp.Net WebAPI的路由方式和Asp.Net MVC是 ...