声明


  想看加强版的戳这里(施工ing,作者正努力中)~

  先贴题目吧哎~   AC自动机加强版  洛谷 P3796


  题目: 洛谷 P3808 (数据范围困了我好久 TAT)

  反正涉及字符串的算法都很玄学,此模板不例外,能用到此模板的都至少 省选+  了。

  

  所需知识点:KMP、Trie。


  由于本人比较无能,忘了以前怎么理解的(包括 KMP 和 Trie),完全忘了,只找到模板,只会套用,等我理解了再来补坑吧!!~

  实在要看思路的这里有传送门:洛谷日报 44 期


  只贴标程(以后再补,一个模板贼长了):

 type
node=record
sum,failed:longint;
son:array ['a'..'z'] of longint;
end;
var
t:array [..] of node;
d,v:array[..] of longint;
s:array[..] of char;
n,len,tot,ans,i:longint;
procedure insert;
var
root,i:longint;
begin
root:=;
for i:= to len do
begin
if t[root].son[s[i]]= then
begin
inc(tot);
t[tot].failed:=-;
t[root].son[s[i]]:=tot;
end;
root:=t[root].son[s[i]];
end;
inc(t[root].sum);
end;
procedure bfs;
var
h,r,now,s,f:longint;
ch:char;
begin
h:=;
r:=;
while h<r do
begin
now:=d[h];
for ch:='a' to 'z' do
begin
s:=t[now].son[ch];
if s<> then
begin
f:=t[now].failed;
while (f<>-) and (t[f].son[ch]=) do
f:=t[f].failed;
if f=- then t[s].failed:=
else t[s].failed:=t[f].son[ch];
d[r]:=s;
inc(r);
end;
end;
inc(h);
end;
end;
procedure AC_Automaton;
var
i,now,k,x:longint;
begin
i:=;
now:=;
while i<=len do
begin
k:=t[now].son[s[i]];
if k<> then
begin
x:=k;
while (v[x]=) and (x<>) do
begin
v[x]:=;
inc(ans,t[x].sum);
x:=t[x].failed;
end;
now:=k;
inc(i);
end else
begin
x:=now;
while (x<>-) and (t[x].son[s[i]]=) do
x:=t[x].failed;
now:=x;
if now=- then
begin
now:=;
inc(i);
end;
end;
end;
end;
begin
readln(n);
t[].failed:=-;
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;
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;
AC_Automaton;
writeln(ans);
end.

AC_Automaton

AC自动机(简单版)(施工ing)的更多相关文章

  1. [模板][P3808]AC自动机(简单版)

    Description: 求n个模式串中有几个在文本串中出现 Solution: 模板,详见代码: #include<bits/stdc++.h> using namespace std; ...

  2. Ring HDU - 2296 AC自动机+简单DP和恶心的方案输出

    题意: 就是现在给出m个串,每个串都有一个权值,现在你要找到一个长度不超过n的字符串, 其中之前的m个串每出现一次就算一次那个字符串的权值, 求能找到的最大权值的字符串,如果存在多个解,输出最短的字典 ...

  3. POJ 1625 Censored!(AC自动机->指针版+DP+大数)题解

    题目:给你n个字母,p个模式串,要你写一个长度为m的串,要求这个串不能包含模式串,问你这样的串最多能写几个 思路:dp+AC自动机应该能看出来,万万没想到这题还要加大数...orz 状态转移方程dp[ ...

  4. 小明系列故事――女友的考验 HDU - 4511 AC自动机+简单DP

    题意:自己看题目,中文体面. 题解: 把所有不能走的路径放入AC自动机中. 然后DP[i][j]表示走到 i 这个点,且位于AC自动机 j 这个节点最短距离 然后直接DP即可.注意一点会爆int #i ...

  5. Walk Through Squares HDU - 4758 AC自动机+简单状压DP

    题意:给你两个串,求用m个R,n个D能组成多少个包含这两个串 题解:先构造一个AC自动机记录每个状态包含两个串的状态, 状态很容易定义 dp[i][j][k][status]表示在AC自动机K这个节点 ...

  6. Censored! - POJ 1625(ac自动机+简单dp+高精度运算)

    题目大意:首先给一个字符集合,这个集合有N个字符,然后需要一个长度为M的句子,但是据子里面不能包含的串有P个,每个串里面的字符都是有字符集和里面的字符构成的,现在想知道最多能构造多少个不重复的句子. ...

  7. AC自动机例题

    P3808 [模板]AC自动机(简单版) [题目描述] 给定n个模式串和1个文本串,求有多少个模式串在文本串里出现过. #include<bits/stdc++.h> using name ...

  8. [C#] 逆袭——自制日刷千题的AC自动机攻克HDU OJ

    前言 做过杭电.浙大或是北大等ACM题库的人一定对“刷题”不陌生,以杭电OJ为例:首先打开首页(http://acm.hdu.edu.cn/),然后登陆,接着找到“Online Exercise”下的 ...

  9. 从Trie谈到AC自动机

    ZJOI的SAM让我深受打击,WJZ大神怒D陈老师之T3是SAM裸题orz...我还怎么混?暂且写篇`从Trie谈到AC自动机`骗骗经验. Trie Trie是一种好玩的数据结构.它的每个结点存的是字 ...

随机推荐

  1. ESS控制台发布新功能:创建多实例规格的伸缩配置

    背景 原弹性伸缩ESS服务限定,生效的伸缩配置中只能对应一种实例规格,这样就会存在如果生效的配置中的实例规格的库存不足(高配实例规格通常更容易出现库存不足的情况)时, 用户配置好的伸缩规则以及伸缩组对 ...

  2. 深入浅出SharePoint——Caml快速开发

    适用于Visual Studio 2010的Caml智能感知工具 http://visualstudiogallery.msdn.microsoft.com/15055544-fda0-42db-a6 ...

  3. python使用SQLAlchemy对mysql操作

    安装SQLAlchemy pip install sqlalchemy 在MySQL的test数据库中创建的user表,用SQLAlchemy来试试 数据库连接 第一步,导入SQLAlchemy,并初 ...

  4. 字符串String及字符Char的相关方法

    一.字符串: 1.访问String中的字符: string本身可看作一个Char数组. string s = "hello world"; ; i < s.Length; i ...

  5. 基于session和cookie的登录验证(CBV模式)

    基于session和cookie的登录验证(CBV模式) urls.py """cookie_session URL Configuration The `urlpatt ...

  6. Python常用库之三:Matplotlib

    导入模块 import matplotlib.pyplot as plt import seaborn as sb 绘制条形图 countplot(data:数据集, x:x坐标轴, color:条形 ...

  7. C#关于排程举一个小例子

    执行后程序会一直执行. 下面是一个例子

  8. [转]对象映射类AutoMapper的使用

    由于原文太长了,此处就直接贴上原文的超链接,大家自行学习. codeproject中的一篇文章: AutoMapper

  9. 枚举enum和enumerate

    #coding=utf-8 from enum import Enum #定义自己的枚举时需要使用class,继承Enum类 class Color(Enum): red=1 green=2 blue ...

  10. php多进程编程实现与优化

    PHP多进程API 创建子进程 @params void @returns int int pcntl_fork(void) 成功时,在父进程执行线程内返回产生的子进程PID,在子进程执行线程内返回0 ...