Codechef2015 May - Chef and Strings (后缀自动机)
用后缀自动机统计出出现1~n次的串的数量f[i]
对于ans[k]=sigma(f[i]*C(i,k)) i>=k
const maxn=;
mo=;
var a,f,rig:array[..maxn] of dword;
nt:array[..maxn,'a'..'z'] of longint;
last,sum,i:dword;
s:ansistring;
eg:array[..maxn*] of record nt,v:dword; end;
lt:array[..maxn] of dword;
el:dword; time:array[..maxn] of qword;
T,j,TT:dword;
ans:array[..maxn] of qword;
C:array[-..,-..] of qword;
du,g:array[..maxn] of longint;
b:array[..] of longint;
procedure SAM_init; inline;
begin
fillchar(f,sizeof(f),);
fillchar(nt,sizeof(nt),);
fillchar(a,sizeof(a),);
fillchar(rig,sizeof(rig),);
el:=;
fillchar(lt,sizeof(lt),);
fillchar(ans,sizeof(ans),);
fillchar(du,sizeof(du),);
fillchar(g,sizeof(g),);
last:=; sum:=;
end; procedure SAM_ins(ch:char); inline;
var next,p,np,q,nq:longint;
begin
inc(sum); p:=last; np:=sum; a[np]:=a[p]+; last:=np; rig[np]:=;
while (p<>-) and (nt[p][ch]=-) do begin nt[p][ch]:=np; p:=f[p]; end;
if p=- then f[np]:= else
begin
q:=nt[p][ch];
if a[p]+=a[q] then f[np]:=q else
begin
inc(sum); nq:=sum; a[nq]:=a[p]+;
nt[nq]:=nt[q];
f[nq]:=f[q]; f[q]:=nq; f[np]:=nq;
while (p<>-) and (nt[p][ch]=q) do begin nt[p][ch]:=nq; p:=f[p]; end;
end;
end;
end; procedure SAM_visit1; inline;
var i,l,r:longint;
c:char;
begin
for i:= to sum do
for c:='a' to 'z' do
if nt[i][c]<>- then inc(du[nt[i][c]]); l:=; r:=; b[]:=; g[]:=;
while l<=r do
begin
for c:='a' to 'z' do
if nt[b[l]][c]<>- then
begin
dec(du[nt[b[l]][c]]);
inc(g[nt[b[l]][c]],g[b[l]]);
if du[nt[b[l]][c]]= then
begin
inc(r);
b[r]:=nt[b[l]][c];
end;
end;
inc(l);
end;
for i:= to sum do inc(time[rig[i]],g[i]);
end;
procedure dfs(u:dword);
var i:dword;
begin
i:=lt[u];
while i<> do
begin
dfs(eg[i].v);
rig[u]:=rig[u]+rig[eg[i].v];
i:=eg[i].nt;
end;
end;
procedure add(u,v:dword); inline;
begin
inc(el);
eg[el].v:=v;
eg[el].nt:=lt[u];
lt[u]:=el;
end;
procedure SAM_rig; inline;
begin
el:=;
fillchar(lt,sizeof(lt),);
for i:= to sum do add(f[i],i);
dfs();
end;
procedure main; inline;
var n,q,i,j:dword;
cnt:qword;
begin
if T<> then
begin
fillchar(time,sizeof(time),);
SAM_init;
end;
readln(n,q);
readln(s);
SAM_init;
for i:= to n do SAM_ins(s[i]);
SAM_rig;
SAM_visit1;
for i:= to n do
for j:=i to n do ans[i]:=(ans[i]+C[j,i]*time[j]) mod mo;
for i:= to q do
begin
readln(j);
if j>n then writeln() else
writeln(ans[j]);
end;
end;
begin
C[,]:=;
for i:= to do
for j:= to i do
begin
C[i,j]:=(c[i-,j-]+c[i-,j]);
if C[i,j]>=mo then C[i,j]:=C[i,j]-mo;
end;
readln(T);
for TT:= to T do main;
end.
Codechef2015 May - Chef and Strings (后缀自动机)的更多相关文章
- HDU 6208 The Dominator of Strings 后缀自动机
The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java ...
- Codeforces 452E Three Strings(后缀自动机)
上学期很认真地学了一些字符串的常用工具,各种 suffix structre,但是其实对后缀自动机这个部分是理解地不太透彻的,以致于看了师兄A这题的代码后,我完全看不懂,于是乎重新看回一些学习后缀自动 ...
- CF 149E Martian Strings 后缀自动机
这里给出来一个后缀自动机的题解. 考虑对 $s$ 的正串和反串分别建后缀自动机. 对于正串的每个节点维护 $endpos$ 的最小值. 对于反串的每个节点维护 $endpos$ 的最大值. 这两个东西 ...
- 字符串(后缀自动机):Codeforces Round #129 (Div. 1) E.Little Elephant and Strings
E. Little Elephant and Strings time limit per test 3 seconds memory limit per test 256 megabytes inp ...
- CodeForces - 616F:Expensive Strings (后缀自动机)
You are given n strings ti. Each string has cost ci. Let's define the function of string , where ps, ...
- CodeForces-204E:Little Elephant and Strings (广义后缀自动机求出现次数)
The Little Elephant loves strings very much. He has an array a from n strings, consisting of lowerca ...
- E. Three strings 广义后缀自动机
http://codeforces.com/problemset/problem/452/E 多个主串的模型. 建立一个广义后缀自动机,可以dp出每个状态的endpos集合大小.同时也维护一个R[]表 ...
- codeforces 204E. Little Elephant and Strings(广义后缀自动机,Parent树)
传送门在这里. 大意: 给一堆字符串,询问每个字符串有多少子串在所有字符串中出现K次以上. 解题思路: 这种子串问题一定要见后缀自动机Parent树Dfs序统计出现次数都是套路了吧. 这道题统计子串个 ...
- CF452E Three strings 广义后缀自动机
建一个广义后缀自动机统计一下就行,好长时间不敲后缀自动机调了半天~ #include <bits/stdc++.h> using namespace std; namespace IO { ...
随机推荐
- FireFox 一键清理缓存
https://addons.mozilla.org/zh-CN/firefox/addon/empty-cache-button/
- CRM创建物料FM2
这是在佛山好帮手时受启发而研究出来的,创建物料,带单位,类型组 经测试....算了,不说了,有什么限制自己测去...今天心情不好... FUNCTION ZLY_CREATE_PRODUCT_UNIT ...
- Laravel
Laravel是一套简洁.优雅的PHP Web开发框架(PHP Web Framework).它可以让你从面条一样杂乱的代码中解脱出来:它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁.富于 ...
- backbonejs中的模型篇(二)
一:模型标识符 每个模型都有一个用作唯一标识符的ID属性,以便在不同模型间进行区分.通过id属性我们可以直接访问模型对象当中用于标识符存放的属性,默认属性名为id,但也可以通过设置idAttribut ...
- Objective-C(NSString、BOOL、多文件开发)
NSString 表示oc当中的字符串类 %@是oc当中对象的格式符 printf不能打印oc当中的对象 通过stringWithFormat:这个类方法,打印格式化的字符串 例 int a = 10 ...
- PHP分页类库
<?php /** * @title: Ekcms page分页类库 * @version: 1.0 * @author: perry <perry@1kyou.com> * @pu ...
- 第五章 CSS页面布局基础
1.标准文档流 在正常流中,在没有使用浮动或者定位的情况下,文本元素按照从上到下.从左到右的格式布局.这是浏览器的默认行为.在正常流中,块级元素从上到下依次排列,而行级元素从左到右依次排列.正常流中的 ...
- [vijos P1112] 小胖的奇偶
第一次看到这题怎么也不会想到是并查集题目…星期五第一次看到这题,到今天做出来,实在是废了好多功夫.看了很多人的解题都有same和diff数组,我也写了,后来发现不对啊两个数组的话find函数怎么写呢? ...
- android shape详解
shape--> shape属性: rectangle: 矩形,默认的形状,可以画出直角矩形.圆角矩形.弧形等 solid: 设置形状填充的颜色,只有android:color一个属性 andr ...
- android 定时器的使用
1.android中通常是使用AlarmManager来定时启动一个单次或重复多次操作的.具体的说就是我们通过AlarmManager设定一个时间和注册一个intent到系统中,然后在该时间到来时,系 ...