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 { ...
随机推荐
- iOS开发之——从零开始完成页面切换形变动画
前言 某天我接到了UI发给我的两张图: 需求图.png 看到图的时候我一脸懵逼,显然我需要做一个页面切换的指示动画.老实说,从大三暑假开始做iOS开发也一年有余了,但是遇到复杂动画总是唯恐避之不及,只 ...
- JSON 换行、JSON \r\n、怎么处理 ?(转载)
参考地址: http://www.cnblogs.com/mamingbo/archive/2010/11/27/1889583.html 在最后json的字符串上:.Replace("\r ...
- 再谈HTML
关于WEB 采用B/S计算模式开发的应用程序我们一般称为Web应用程序. WEB三大层面: 网页的结构部分:结构的定义使用HTML语言(超文本标记语言Hyper Text Mark Up Langua ...
- arrow css
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Go语言并发与并行学习笔记(一)
转:http://blog.csdn.net/kjfcpua/article/details/18265441 如果不是我对真正并行的线程的追求,就不会认识到Go有多么的迷人. Go语言从语言层面上就 ...
- oracle常见权限分配
1.GRANT 赋于权限 常用的系统权限集合有以下三个: CONNECT(基本的连接), RESOURCE(程序开发), DBA(数据库管理) 常用的数据对象权限有以下五个: ALL ON 数据对象名 ...
- cisco 路由配置
Cisco路由配置基础 刚刚接触cisco路由配置,下面是学习的笔记,感觉命令还是多敲才能熟悉 一. 所处状态各类 router> 用户处于用户命令状态,可以查看网络和主机 router# 用户 ...
- Windows API 文件处理
CloseHandle 关闭一个内核对象.其中包括文件.文件映射.进程.线程.安全和同步对象等 CompareFileTime 对比两个文件的时间 CopyFile 复制文件 CreateDirect ...
- 读懂diff
作者: 阮一峰 日期: 2012年8月29日 diff是Unix系统的一个很重要的工具程序. 它用来比较两个文本文件的差异,是代码版本管理的基石之一.你在命令行下,输入: $ diff <变动前 ...
- Android Webview 背景透明
两个关键点: 1 fBarParams.format = PixelFormat.RGBA_8888; 2 mWebView.setBackgroundColor(Color.TRAN ...