HDU2222 (AC自动机)

AC自动机模板题。

被卡内存了 死活A不掉。。

AC自动机参考教程:

  http://www.cppblog.com/menjitianya/archive/2014/07/10/207604.html

 1 const maxn=10008;
 2 type arr=record
 3         next:array[0..26] of longint;
 4         fail,cnt:longint;
 5         end;
 6 var cas:longint;
 7     s:array[0..maxn] of string;
 8     T:array[0..200000] of arr;
 9     id:array[‘a‘..‘z‘] of longint;
10     rt,tmp:longint;
11     b:array[0..100000] of longint;
12 procedure ins(str:string);
13 var now,i:longint;
14 begin
15     now:=rt;
16     for i:=1 to length(str) do
17     begin
18         if T[now].next[id[str[i]]]=0 then
19         begin
20             inc(tmp);
21             T[now].next[id[str[i]]]:=tmp;
22         end;
23         now:=T[now].next[id[str[i]]];
24     end;
25     inc(T[now].cnt);
26 end;
27 procedure build;
28 var l,r,u,v,i:longint;
29 begin
30     fillchar(b,sizeof(b),0);
31     r:=0;
32     for i:=1 to 26 do
33         if T[rt].next[i]<>0 then
34         begin
35             T[T[rt].next[i]].fail:=rt;
36             inc(r);
37             b[r]:=T[rt].next[i];
38         end;
39     l:=1;
40     while l<=r do
41     begin
42         u:=b[l];
43         for i:=1 to 26 do
44         begin
45             v:=T[u].next[i];
46             if v=0 then
47                 T[u].next[i]:=T[T[u].fail].next[i]
48             else
49             begin
50                 T[v].fail:=T[T[u].fail].next[i];
51                 inc(r);
52                 b[r]:=v;
53             end;
54         end;
55         inc(l);
56     end;
57 end;
58 function query(st:ansistring):longint;
59 var i,ans,now,j:longint;
60 begin
61     ans:=0; now:=rt;
62     for i:=1 to length(st) do
63     begin
64         now:=T[now].next[id[st[i]]];
65         j:=now;
66         while T[j].cnt>0 do
67         begin
68             ans:=ans+T[j].cnt;
69             T[j].cnt:=0;
70             j:=T[j].fail;
71         end;
72         ans:=ans+T[T[j].fail].cnt;
73         T[T[j].fail].cnt:=0;
74     end;
75     exit(ans);
76 end;
77 procedure main;
78 var i,n:longint;
79     st:ansistring;
80 begin
81     fillchar(T,sizeof(T),0);
82     for i:=1 to 26 do id[chr(96+i)]:=i;
83     readln(n);
84     for i:=1 to n do readln(s[i]);
85     rt:=0; tmp:=0;
86     for i:=1 to n do ins(s[i]);
87     build;
88     readln(st);
89     writeln(query(st));
90 end;
91 begin
92     readln(cas);
93     while cas>0 do begin dec(cas); main; end;
94 end.
时间: 2024-10-10 17:51:47

HDU2222 (AC自动机)的相关文章

hdu2222 AC自动机-给定串中出现了几个模式串

http://acm.hdu.edu.cn/showproblem.php?pid=2222 Problem Description In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey also wants to bring this feature to his image retrieval system. Every image have a lo

HDU2222 AC自动机

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 59781    Accepted Submission(s): 19700 Problem Description In the modern time, Search engine came into the life of everybody lik

hdu2222(ac自动机模板)

先推荐两篇写的很好的ac自动机blog: http://blog.csdn.net/creatorx/article/details/71100840 http://blog.csdn.net/niushuai666/article/details/7002823 正题 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意: 给出 n 个模式串以及一个 主串, 问有多少个模式串在主串中出现过 思路: ac自动机模板题 代码: 1 #inc

[hdu2222]ac自动机(模板)

题意:一个文本串+多个模板串的匹配问题 思路:裸的ac自动机. 1 #pragma comment(linker, "/STACK:10240000,10240000") 2 3 #include <iostream> 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstdlib> 7 #include <cstring> 8 #include <map&g

[hdu2222] [AC自动机模板] Keywords Search [AC自动机]

AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! 1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <ctime> 7 #include <cstdlib> 8 #include <queue>

hdu2222 AC自动机入门

其实很久以来我都把sam当成ac自动机了 ac自动机的建立比sam简单多了 直接暴力建字典树,然后暴力跑fail就好了 (挖个坑:去学fail树) 裸题A一道,岂不美哉 1 #include <bits/stdc++.h> 2 #define MAX 1000000 3 using namespace std; 4 int T,n;char ch; 5 struct acm 6 { 7 int cnt,h,t,c[MAX][26],fail[MAX],que[MAX],ret[MAX]; 8

Keywords Search HDU2222 AC自动机模板题

ac自动机说起来很复杂,其实和kmp是一样的思路,都是寻找相同前后缀,减少跳的次数.只要理解了kmp是怎么求next数组的,ac自动机bfs甚至比knp还好写. 这里大致说一下kmp求next数组的方法吧,假设现在要求第c个字符的next值(假设这个c很大,这样画图出来比较清晰方便理解),因为遍历过程中我们已经知道了第c-1个字符的next为x(假设比c小很多),即next[c-1] = x.那就代表我们知道了a[1]-a[x]这一段和a[c-1-x]-a[c-1]这一段是相等的对吧. 那么现在

// hdu2222 // AC自动机初学

// hdu2222 // #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<iostream> #include<algorithm> #include<queue> using namespace std; char k[55],s[1000005]; struct node { int fail; int nex

【HDU2222】Keywords Search(AC自动机)

Problem Description In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.Wiskey also wants to bring this feature to his image retrieval system.Every image have a long description, when users type some keywords to