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是一种好玩的数据结构.它的每个结点存的是字 ...
随机推荐
- Compare DML To Both REDO And UNDO Size
SUMMARY you can remember undo rule the same to redo if you want demo rule that you can look up the ...
- HAProxy负载均衡保持客户端和服务器Session亲缘性的3种方式
1 用户IP 识别 haroxy 将用户IP经过hash计算后 指定到固定的真实服务器上(类似于nginx 的IP hash 指令) 配置指令: balance source 配置实例: backe ...
- 沉淀再出发:jvm的本质
沉淀再出发:jvm的本质 一.前言 关于jvm,使用的地方实在是太多了,从字面意思上我们都能明白这也是一个虚拟机,那么其他的虚拟机都会用来运行别的操作系统的,而jvm却是实现了可以在不用的操作系统之上 ...
- Retrieving failed records after an SqlBulkCopy exception
Let me start by saying that the idea I used in this article is not originally mine, but since I have ...
- August 27th 2017 Week 35th Sunday
You can't be brave if you've only had wonderful things happen to you. 人生若只是有美好的境遇,那你也没办法学会勇敢. Wherea ...
- HomeBrew 使用国内数据源
使用中科大源 1.替换默认源 替换USTC镜像: cd "$(brew --repo)" git remote set-url origin https://mirrors.ust ...
- python异常处理及内置模块
异常处理 有时候我们在写程序的时候会出现错误或者异常,导致程序终止,如下这个例子: #!/usr/bin/env python a = 2/0 print(a) 结果提示如下错误: Traceback ...
- Client/Server 模型 与socket
Client/Server 模型 Sockets 是以 Client 和 Server 交互通信方式来使用的.典型的系统配置是把 Server 放在一台机器中,而把 Client 放在另一台机器中, ...
- ZOJ1081 Points Within
嘟嘟嘟 题面:给一个\(n\)个点的多边形和\(m\)个点,判断每一个点是否在多边形内. 解法:射线法. 就是从这个点引一条射线,如果与多边形有奇数个交点,则在多边形内部. 那么只用枚举每一条边,然后 ...
- 【bbs】logout.php
字体大小通过js设定,并结合@media,可实现自适应. 图片自适应窗口 实现流水灯手机端不滚动,script嵌套 多余文字省略号显示 http://www.cnblogs.com/yujihang ...