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 { ...
随机推荐
- android 圆角边框及图片
<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http: ...
- java操作字符串,重点是记录一次使用replaceAll对字符串的操作
符串常用操作(String类) 字符串查找 String提供了两种查找字符串的方法,即indexOf与lastIndexOf方法. 1.indexOf(String s) 该方法用于返回参数字符串s在 ...
- 对于transform的新认识
transform-origin是作用于该元素自己的,transform-origin(0px,0px),是以该元素自己本身的左上角(0px,0px)为圆心进行动作的.
- Protocol Buffers in HBase
For early Hbase developers, it is often a nightmare to understand how the different modules speak am ...
- [示例]NSDictionary编程题-字典的排序应用(iOS5班)
代码? #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepo ...
- 时钟 IoTimer
/* 例程是在运行在DISPATCH_LEVEL的IRQL级别 例程中不能使用分页内存 另外在函数首部使用 #pragma LOCKEDCODE */ #include "Driver.h& ...
- S1:对象与JSON
JSON全称为JavaScript对象表示法(JavaScript Object Notation). JSON是JavaScript中对象的字面量,是对象的表示方法,通过使用JSON,可以减少中间变 ...
- hdu 4611 Balls Rearrangement
http://acm.hdu.edu.cn/showproblem.php?pid=4611 从A中向B中移动和从B中向A中移动的效果是一样的,我们假设从B中向A中移动 而且A>B 我们先求出所 ...
- 佳佳的魔杖 (vijos 1283)
题目大意: 一根树枝有N段,每一段有一个分数,可以选取一些不完全包含(可以相交)的区间,每次选取可以得到区间里所有数之和的分数. 求最大得分. 解题过程: 1.很明显的dp,默认选取区间的顺序是从左往 ...
- svnadmin:error while loading shared libraries: libaprutil-1.so.0:cannot open shared object file: No such file or directory
wdcp下安装svn后一直提示 svnadmin:error while loading shared libraries: libaprutil-1.so.0:cannot open shared ...