AC自动机(简单版)(施工ing)
想看加强版的戳这里(施工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)的更多相关文章
- [模板][P3808]AC自动机(简单版)
Description: 求n个模式串中有几个在文本串中出现 Solution: 模板,详见代码: #include<bits/stdc++.h> using namespace std; ...
- Ring HDU - 2296 AC自动机+简单DP和恶心的方案输出
题意: 就是现在给出m个串,每个串都有一个权值,现在你要找到一个长度不超过n的字符串, 其中之前的m个串每出现一次就算一次那个字符串的权值, 求能找到的最大权值的字符串,如果存在多个解,输出最短的字典 ...
- POJ 1625 Censored!(AC自动机->指针版+DP+大数)题解
题目:给你n个字母,p个模式串,要你写一个长度为m的串,要求这个串不能包含模式串,问你这样的串最多能写几个 思路:dp+AC自动机应该能看出来,万万没想到这题还要加大数...orz 状态转移方程dp[ ...
- 小明系列故事――女友的考验 HDU - 4511 AC自动机+简单DP
题意:自己看题目,中文体面. 题解: 把所有不能走的路径放入AC自动机中. 然后DP[i][j]表示走到 i 这个点,且位于AC自动机 j 这个节点最短距离 然后直接DP即可.注意一点会爆int #i ...
- Walk Through Squares HDU - 4758 AC自动机+简单状压DP
题意:给你两个串,求用m个R,n个D能组成多少个包含这两个串 题解:先构造一个AC自动机记录每个状态包含两个串的状态, 状态很容易定义 dp[i][j][k][status]表示在AC自动机K这个节点 ...
- Censored! - POJ 1625(ac自动机+简单dp+高精度运算)
题目大意:首先给一个字符集合,这个集合有N个字符,然后需要一个长度为M的句子,但是据子里面不能包含的串有P个,每个串里面的字符都是有字符集和里面的字符构成的,现在想知道最多能构造多少个不重复的句子. ...
- AC自动机例题
P3808 [模板]AC自动机(简单版) [题目描述] 给定n个模式串和1个文本串,求有多少个模式串在文本串里出现过. #include<bits/stdc++.h> using name ...
- [C#] 逆袭——自制日刷千题的AC自动机攻克HDU OJ
前言 做过杭电.浙大或是北大等ACM题库的人一定对“刷题”不陌生,以杭电OJ为例:首先打开首页(http://acm.hdu.edu.cn/),然后登陆,接着找到“Online Exercise”下的 ...
- 从Trie谈到AC自动机
ZJOI的SAM让我深受打击,WJZ大神怒D陈老师之T3是SAM裸题orz...我还怎么混?暂且写篇`从Trie谈到AC自动机`骗骗经验. Trie Trie是一种好玩的数据结构.它的每个结点存的是字 ...
随机推荐
- 新特性之MAPI over HTTP \ 配置 MAPI over HTTP
Exchange 2016 中的 MAPI over HTTP https://docs.microsoft.com/zh-cn/Exchange/clients/mapi-over-http/map ...
- Apache配置支持目录浏览
主配置文件 httpd.conf 中修改: 1)添加支持模块 LoadModule autoindex_module modules/mod_autoindex.so LoadModule dir ...
- DRAM(动态)存储器
一.DRAM的存储元电路 常见的DRAM存储元电路有四管式和单管式两种,它们的共同特点是靠电容存储电荷的原理来存储信息.电容上存有足够多的电荷表示“1”,电容上无电荷表示“0”. 由于电容存储的电荷会 ...
- C#网络编程(二)应用篇
(一)TcpListen类.TcpClient类 TcpListener类和TcpClient类都是System.Net.Sockets命名空间下的类,利用TcpListener和TcpClient可 ...
- codeforces 549F Yura and Developers(分治、启发式合并)
codeforces 549F Yura and Developers 题意 给定一个数组,问有多少区间满足:去掉最大值之后,和是k的倍数. 题解 分治,对于一个区间,找出最大值之后,分成两个区间. ...
- Jarsigner签名使用
转载请标明出处: http://www.cnblogs.com/why168888/p/6548544.html 本文出自:[Edwin博客园] 如何签名: jarsgner-verbose-keys ...
- HashMap源代码解析
HashMap原理剖析 之前有看过别人的HashMap源代码的分析,今天尝试自己来分析一波,纯属个人愚见.听一些老的程序员说过,当别人跟你说用某样技术到项目中去,而你按照别人的想法实现了的时候,你只能 ...
- virtualbox+vagrant学习-4-Vagrantfile-3-Minimum Vagrant Version
Minimum Vagrant Version 可以在Vagrantfile中指定一组vagrant版本需求,以强制人们使用带有Vagrantfile文件的vagrant特定版本.这可以帮助解决使用带 ...
- 更有效率的使用Visual Studio
工欲善其事,必先利其器.虽然说Vim和Emacs是神器,但是对于使用Visual Studio的程序员来说,我们也可以通过一些快捷键和潜在的一些功能实现脱离鼠标写代码,提高工作效率,像使用Vim一样使 ...
- 使用java代码将时间戳和时间互相转换
时间戳转时间: SimpleDateFormat simpleDateFormat = null; simpleDateFormat = new SimpleDateFormat("yyyy ...