bzoj3172
这里学习AC自动机
其实对KMP和trie掌握好了之后很容易扩展成AC自动机的
这里运用了一个性质
由失配指针反向可以建成一棵fail树
x串在y串中的出现的次数即为在fail树上以x结尾节点为根的子树中有多少个节点属于y串,这很好理解
而推广到这题,在文章中出现的次数即为单词结尾节点为根的子树的规模
注意这道题有相同的单词
当初这题我是用后缀数组做的O(LlogL)
而自动机复杂度为O(L),快了很多
- type node=record
- po,next:longint;
- end;
- var e:array[..] of node;
- trie:array[..,'a'..'z'] of longint;
- q,f,p,w:array[..] of longint;
- v:array[..] of longint;
- t,i,j,k,n,l,len:longint;
- s:ansistring;
- procedure add(x,y:longint);
- begin
- inc(len);
- e[len].po:=y;
- e[len].next:=p[x];
- p[x]:=len;
- end;
- procedure bfs;
- var x,y,h,r,j:longint;
- c:char;
- begin
- h:=;
- r:=;
- for c:='a' to 'z' do
- if trie[,c]> then
- begin
- add(,trie[,c]);
- inc(r);
- q[r]:=trie[,c];
- end;
- while h<=r do
- begin
- x:=q[h];
- for c:='a' to 'z' do
- if trie[x,c]> then
- begin
- y:=trie[x,c];
- inc(r);
- q[r]:=y;
- j:=f[x];
- while (j>) and (trie[j,c]=) do j:=f[j];
- f[y]:=trie[j,c];
- add(trie[j,c],y);
- end;
- inc(h);
- end;
- end;
- procedure dfs(x:longint);
- var i,y:longint;
- begin
- i:=p[x];
- while i<> do
- begin
- y:=e[i].po;
- dfs(y);
- w[x]:=w[y]+w[x];
- i:=e[i].next;
- end;
- end;
- begin
- readln(n);
- for i:= to n do
- begin
- readln(s);
- j:=;
- l:=length(s);
- for k:= to l do
- begin
- if trie[j,s[k]]= then
- begin
- inc(t);
- trie[j,s[k]]:=t;
- end;
- j:=trie[j,s[k]];
- inc(w[j]);
- end;
- v[i]:=j;
- end;
- bfs;
- dfs();
- for i:= to n do
- writeln(w[v[i]]);
- end.
ac自动机
- var h,rank,x,y,sum,sa:array[..] of longint;
- f:array[..,..] of longint;
- where,len,d:array[..] of longint;
- now, k,ans,i,j,p,n,m,t,l,r:longint;
- s,cc:ansistring;
- function min(a,b:longint):longint;
- begin
- if a>b then exit(b) else exit(a);
- end;
- function lcp(a,b:longint):longint;
- var k:longint;
- begin
- k:=trunc(ln(b-a+)/ln());
- exit(min(f[a,k],f[b-d[k]+,k]));
- end;
- begin
- readln(t);
- for i:= to t do
- begin
- readln(cc);
- where[i]:=length(s)+;
- len[i]:=length(cc);
- s:=s+cc;
- if i<>t then s:=s+' ';
- end;
- { for i:=1 to t do
- writeln(where[i],' ',len[i]);}
- n:=length(s);
- for i:= to n do
- begin
- y[i]:=ord(s[i]);
- inc(sum[y[i]]);
- end;
- m:=;
- for i:= to do
- inc(sum[i],sum[i-]);
- for i:= to n do
- begin
- sa[sum[y[i]]]:=i;
- dec(sum[y[i]]);
- end;
- p:=;
- rank[sa[]]:=;
- for i:= to n do
- begin
- if y[sa[i]]<>y[sa[i-]] then inc(p);
- rank[sa[i]]:=p;
- end;
- m:=p;
- j:=;
- while m<n do
- begin
- y:=rank;
- fillchar(sum,sizeof(sum),);
- p:=;
- for i:=n-j+ to n do
- begin
- inc(p);
- x[p]:=i;
- end;
- for i:= to n do
- if sa[i]>j then
- begin
- inc(p);
- x[p]:=sa[i]-j;
- end;
- for i:= to n do
- begin
- rank[i]:=y[x[i]];
- inc(sum[rank[i]]);
- end;
- for i:= to m do
- inc(sum[i],sum[i-]);
- for i:=n downto do
- begin
- sa[sum[rank[i]]]:=x[i];
- dec(sum[rank[i]]);
- end;
- p:=;
- rank[sa[]]:=;
- for i:= to n do
- begin
- if (y[sa[i]]<>y[sa[i-]]) or (y[sa[i]+j]<>y[sa[i-]+j]) then inc(p);
- rank[sa[i]]:=p;
- end;
- j:=j shl ;
- m:=p;
- end;
- h[]:=;
- p:=;
- for i:= to n do
- begin
- if rank[i]= then continue;
- j:=sa[rank[i]-];
- while s[i+p]=s[j+p] do inc(p);
- h[rank[i]]:=p;
- if p> then dec(p);
- end;
- k:=trunc(ln(n)/ln());
- d[]:=;
- for i:= to k do
- d[i]:=d[i-]*;
- for i:= to n do
- f[i,]:=h[i];
- for j:= to k do
- for i:= to n do
- if i+d[j]-<=n then
- begin
- f[i,j]:=min(f[i,j-],f[i+d[j-],j-]);
- end
- else break;
- for i:= to t do
- begin
- ans:=;
- k:=-;
- l:=;
- now:=rank[where[i]];
- r:=now-;
- while l<=r do
- begin
- m:=(l+r) shr ;
- if lcp(m+,now)>=len[i] then
- begin
- k:=m;
- r:=m-
- end
- else l:=m+;
- end;
- if k<>- then ans:=ans+now-k;
- l:=now+;
- k:=-;
- r:=n;
- while l<=r do
- begin
- m:=(l+r) shr ;
- if lcp(now+,m)>=len[i] then
- begin
- k:=m;
- l:=m+;
- end
- else r:=m-;
- end;
- if k<>- then ans:=ans+k-now;
- writeln(ans);
- end;
- end.
后缀数组
bzoj3172的更多相关文章
- 【BZOJ3172】[Tjoi2013]单词 AC自动机
[BZOJ3172][Tjoi2013]单词 Description 某人读论文,一篇论文是由许多单词组成.但他发现一个单词会在论文中出现很多次,现在想知道每个单词分别在论文中出现多少次. Input ...
- 【BZOJ3172】单词(AC自动机)
[BZOJ3172]单词(AC自动机) 题面 Description 某人读论文,一篇论文是由许多单词组成.但他发现一个单词会在论文中出现很多次,现在想知道每个单词分别在论文中出现多少次. Input ...
- BZOJ3172 [Tjoi2013]单词 字符串 SA ST表
原文链接http://www.cnblogs.com/zhouzhendong/p/9026543.html 题目传送门 - BZOJ3172 题意 输入$n(n\leq 200)$个字符串,保证长度 ...
- 【bzoj3172】 Tjoi2013—单词
http://www.lydsy.com/JudgeOnline/problem.php?id=3172 (题目链接) 题意 $n$个单词组成文本,问每个单词在文本中出现了几次. Solution 题 ...
- [BZOJ3172]单词
3172: [Tjoi2013]单词 Time Limit: 10 Sec Memory Limit: 512 MB Description 某人读论文,一篇论文是由许多单词组成.但他发现一个单词会 ...
- 【bzoj3172】: [Tjoi2013]单词 字符串-AC自动机
[bzoj3172]: [Tjoi2013]单词 先用所有单词构造一个AC自动机 题目要求的是每个单词在这个AC自动机里匹配到的次数 每次insert一个单词的时候把路径上的cnt++ 那么点p-&g ...
- BZOJ3172&&lg3966 TJOI单词(广义后缀自动机)
BZOJ3172&&lg3966 TJOI单词(广义后缀自动机) 题面 自己找去 HINT 给出多个文本串,让你查找每个文本串一共出现了多少次,广义后缀自动机建出parent tree ...
- BZOJ3172: [Tjoi2013]单词
传送门 做了这么多题怎么还是无法很好的理解AC自动机呢..果然是个制杖 首先题意表述不是很清晰,这些所有的单词组成了那个文章,所以果断建个AC自动机,建的时候给每个点附加一个权值,建树是经过一次权值即 ...
- BZOJ3172——[Tjoi2013]单词
1. 题目大意:一篇论文是由许多单词组成,现在想知道每个单词分别在论文中出现多少次. 2.分析:对着 广义后缀自动机的图看,我们就会发现玄机,答案不就是这个单词下的后缀个数吗? 于是建立自动机,然后求 ...
- BZOJ3172[Tjoi2013]单词 题解
题目大意: 求一些字符串在一段文章中出现的次数. 思路: AC自动机的经典应用,建完自动机直接将队列里的元素调Fail指针记录即可. 代码: #include<cstdio> #inclu ...
随机推荐
- hadoop_集群安装_1
这篇文章中主要介绍的是,如何基于VM安装Linux,以及如何在安装好Linux之后,基于操作系统安装VMTools. 在安装之前,应该先规划好 每个node*的IP地址,以及 hostname: no ...
- memcache的分布式算法(转)
memcached的分布式 正如第1次中介绍的那样, memcached虽然称为“分布式”缓存服务器,但服务器端并没有“分布式”功能. 服务器端仅包括 第2次. 第3次 前坂介绍的内存存储功能,其实现 ...
- Object-C Dealloc
上一篇为Object-C Init dealloc方法在一个对象从内存中删除时被调用.通常在这个方法里面释放所有对象里的实例变量 -(void)dealloc { [super dealloc]; [ ...
- java新手笔记17 参数
package com.yfs.javase; public class ParamDemo { public static void main(String[] args) { int a = 3, ...
- Ambiguous handler methods mapped for HTTP path
一.问题:映射重复导致的错误 java代码如下: @RequestMapping(value = "/info/{remove}/{id}", method = RequestMe ...
- Redis单机版以及集群版的安装搭建以及使用
1,redis单机版 1.1 安装redis n 版本说明 本教程使用redis3.0版本.3.0版本主要增加了redis集群功能. 安装的前提条件: 需要安装gcc:yum install g ...
- e+开发中的各种问题
1.数据交换后走的查询公式还是controller所配置的公式
- leetcode problem 32 -- Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- leetcode problem (2-4)
Problem 2 --- Add Two Numbers 简单的模拟题. Problem 3 --- Longest Substring Without Repeating Characters 题 ...
- jquery中的 .html(),.val().text()
.html(),.text(),.val(),.html()用为读取和修改元素的HTML标签,包括标签内的内容.text()用来读取或修改元素的纯文本内容,去除 html 标签.val()用来读取或修 ...