51Nod 1600 Simple KMP SAM+LCT/树链剖分
1600 Simple KMP
显然对于一个字符串,如果我们将每个0<=i<=|S|看成一个结点,除了i=0以外i向fail[i]连边,这是一颗树的形状,根是0
我们定义这棵树是G(S),设f(S)是G(S)中除了0号点以外所有点的深度之和,其中0号点的深度为-1
定义key(S)等于S的所有非空子串S'的f(S')之和
给定一个字符串S,现在你要实现以下几种操作:
1.在S最后面加一个字符
2.询问key(S)
善良的出题人不希望你的答案比long long大,所以你需要将答案对1e9+7取模
第一行一个正整数Q
Q<=10^5
第二行一个长度为Q的字符串S
输出Q行,第i行表示前i个字符组成的字符串的答案
5
abaab
0
0
1
4
9
#include < cstdio > #define gec getchar
#define FILE(F) freopen(F".in","r",stdin),freopen(F".out","w",stdout)
#define DEBUG fprintf(stderr,"Passing [%s] in Line (%d)\n",__FUNCTION__,__LINE__) typedef long long ll;
template
inline void read(T&x)
{
x=0;bool f=0;char c=gec();
for(;c<'0'||c>'9';c=gec())f=(c=='-');
for(;c>='0'&&c<='9';c=gec())x=x*10+c-'0';
x=f?-x:x;
}
const int MAXN(100010),MP(1e9+7);
int n;char str[MAXN]; void plus(int &x,int y){x+=y;x-=x>=MP?MP:0;} namespace Force_LCT
{
struct LCT
{
int nx[2],fa;
int step,right;
int Sum_step,Tag,Sum;//Son need +Tag?
}tr[MAXN<<1]; void swap(int &x,int &y){int t(x);x=y;y=t;}
int which(int x){if(tr[tr[x].fa].nx[0]==x)return 0;if(tr[tr[x].fa].nx[1]==x)return 1;return -1;}
void push(int x)
{
if(!tr[x].Tag)return;
plus(tr[tr[x].nx[0]].Tag,tr[x].Tag); plus(tr[tr[x].nx[0]].right,tr[x].Tag);
plus(tr[tr[x].nx[0]].Sum,(ll)tr[x].Tag*tr[tr[x].nx[0]].Sum_step%MP);
plus(tr[tr[x].nx[1]].Tag,tr[x].Tag); plus(tr[tr[x].nx[1]].right,tr[x].Tag);
plus(tr[tr[x].nx[1]].Sum,(ll)tr[x].Tag*tr[tr[x].nx[1]].Sum_step%MP);
tr[x].Tag=0;
} void update(int x)
{
tr[x].Sum=((ll)tr[x].step*tr[x].right)%MP; tr[x].Sum_step=tr[x].step;
if(tr[x].nx[0])plus(tr[x].Sum,tr[tr[x].nx[0]].Sum),plus(tr[x].Sum_step,tr[tr[x].nx[0]].Sum_step);
if(tr[x].nx[1])plus(tr[x].Sum,tr[tr[x].nx[1]].Sum),plus(tr[x].Sum_step,tr[tr[x].nx[1]].Sum_step);
} void rotate(int x)
{
int fa=tr[x].fa,fafa=tr[fa].fa,fd=which(fa),xd=which(x);
tr[tr[x].nx[xd^1]].fa=fa;
tr[fa].nx[xd]=tr[x].nx[xd^1];
tr[x].nx[xd^1]=fa;tr[fa].fa=x;
tr[x].fa=fafa;if(fd!=-1)tr[fafa].nx[fd]=x;
update(fa);
} int st[MAXN<<1],tp;
void splay(int x)
{
st[tp=1]=x;
for(int t=x;which(t)!=-1;t=tr[t].fa)st[++tp]=tr[t].fa;
while(tp)push(st[tp--]);
while(which(x)!=-1)
{
int fa=tr[x].fa;
if(which(fa)!=-1) rotate( which(x)^which(fa)? fa : x );
rotate(x);
}
update(x);
} void access(int x)
{
for(int t=0;x;t=x,x=tr[x].fa)
{
splay(x); tr[x].nx[1]=t; update(x);
}
} void cut(int x,int y)//x's fa is y
{
access(x); splay(y);
tr[y].nx[1]=0; update(y); tr[x].fa=0;
} void link(int x,int y)//x's fa is y
{
splay(y); tr[x].fa=y;
} } namespace Force_SAM
{
using namespace Force_LCT;
struct SAM
{
int nx[26],pre,step,right;
}sam[MAXN<<1];int top=1,now=1,root=1,last,lastson; void New(int x)
{
tr[x].right=sam[x].right;
tr[x].step=sam[x].step-sam[sam[x].pre].step; update(x);
} void entend(int x,int &S,int num)
{
last=now; now=++top; sam[now].step=sam[last].step+1; sam[now].right=1;
for(;!sam[last].nx[x]&&last;last=sam[last].pre)
sam[last].nx[x]=now;
if(!last)sam[now].pre=root;
else
{
lastson=sam[last].nx[x];
if(sam[lastson].step==sam[last].step+1)sam[now].pre=lastson;
else
{
sam[++top]=sam[lastson]; sam[top].step=sam[last].step+1;
splay(lastson); sam[top].right=sam[lastson].right=tr[lastson].right;
New(top); link(top,sam[top].pre);
cut(lastson,sam[lastson].pre);
sam[now].pre=sam[lastson].pre=top;
New(lastson); link(lastson,sam[lastson].pre);
for(;sam[last].nx[x]==lastson&&last;last=sam[last].pre)
sam[last].nx[x]=top;
}
}
New(now);
link(now,sam[now].pre);
access(now); splay(now);
S=tr[tr[now].nx[0]].Sum;
plus(tr[tr[now].nx[0]].Tag,1); plus(tr[tr[now].nx[0]].right,1);
plus(tr[tr[now].nx[0]].Sum,tr[tr[now].nx[0]].Sum_step);
} int F[MAXN];
void Total()
{
New(1);
for(int i=1;i<=n;i++)
{
entend(str[i]-'a',F[i],i);
plus(F[i],F[i-1]);
}
for(int i=1;i<=n;i++)plus(F[i],F[i-1]);
for(int i=1;i<=n;i++)printf("%d\n",F[i]);
} } int main()
{
read(n);scanf("%s",str+1);
Force_SAM::Total();
return 0;
}
51Nod 1600 Simple KMP SAM+LCT/树链剖分的更多相关文章
- 51Nod 1600 Simple KMP 解题报告
51Nod 1600 Simple KMP 对于一个字符串\(|S|\),我们定义\(fail[i]\),表示最大的\(x\)使得\(S[1..x]=S[i-x+1..i]\),满足\((x<i ...
- bryce1010专题训练——LCT&&树链剖分
LCT&&树链剖分专题 参考: https://blog.csdn.net/forever_wjs/article/details/52116682
- 【BZOJ】1036: [ZJOI2008]树的统计Count(lct/树链剖分)
http://www.lydsy.com/JudgeOnline/problem.php?id=1036 lct: (ps:为嘛我的那么慢T_T,不知道排到哪了..难道别人都是树剖吗...看来有必要学 ...
- Cogs 1583. [POJ3237]树的维护 LCT,树链剖分
题目:http://cojs.tk/cogs/problem/problem.php?pid=1583 1583. [POJ3237]树的维护 ★★★☆ 输入文件:maintaintree.in ...
- Cogs 1672. [SPOJ375 QTREE]难存的情缘 LCT,树链剖分,填坑计划
题目:http://cojs.tk/cogs/problem/problem.php?pid=1672 1672. [SPOJ375 QTREE]难存的情缘 ★★★☆ 输入文件:qtree.in ...
- bzoj 4817: [Sdoi2017]树点涂色 LCT+树链剖分+线段树
题目: Bob有一棵n个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同. 定义一条路径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜色. Bob可能会进 ...
- 51nod 1600 Simple KMP【后缀自动机+LCT】【思维好题】*
Description 对于一个字符串|S|,我们定义fail[i],表示最大的x使得S[1..x]=S[i-x+1..i],满足(x<i) 显然对于一个字符串,如果我们将每个0<=i&l ...
- 51nod 1600 Simple KMP
又被机房神犇肉丝哥哥和glory踩爆了 首先这个答案的输出方式有点套路,当前的答案=上一个答案+每一个后缀的f值=上一个答案+上一次算的每个后缀的f值+当前每个后缀的深度 这个题意给了个根深度为-1有 ...
- Bzoj 2243: [SDOI2011]染色 树链剖分,LCT,动态树
2243: [SDOI2011]染色 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 5020 Solved: 1872[Submit][Status ...
随机推荐
- ZOJ3068(01分数规划)
本是POJ2976,喜闻乐见的01规划入门题.POJ日常假死,到ZOJ测. 二分答案. 试了试数据好像没问题,\(a_i\)总是小于\(b_i\)且最终预答案l都小于1.然而为什么我把r设成1e10往 ...
- LDAP环境搭建 OpenLDAP和phpLDAPadmin -- yum版
前言: 前两天公司要求做一个使用LDAP和Kerberos做一个认证授权系统,然后开始学习LDAP相关知识,期间找了不少博客按照步骤来安装,可是很多博客在配置的时候,都会遇到安装过程中一两个问题卡 ...
- buildKibanaServerUrl
private String buildKibanaServerUrl(DiscountIndexMailData mailData,Statistic stat,String failureCaus ...
- 1.Ioc&DI和Spring
1.面向对象回顾和案例 面向对象程序设计:1 2 3 4 案例分析: 需求分析: 报表功能: 报表服务类,检索数据,并生成图标 报表生成器类,生成不同格式的报表文件,例如PDF格式.H ...
- 问题:eclipse中线程编程编译报错,undefined reference to 'pthread_create'的解决方法(已解决)
问题描述: 在Ubuntu系统中,使用eclipse CDT集成开发环境编写pthread程序,编译时,pthread_create不通过,报错信息是: undefined reference to ...
- Mysql 查看表数据以及索引大小
如果想查看 Mysql 数据库的总的数据量或者某个表的数据或者索引大小,可以使用系统库 information_schema 来查询,这个系统库中有一个 TABLES 表,这个表是用来记录数据库中表的 ...
- Spring Boot 实现ErrorController接口处理404、500等错误页面
在项目中我们遇到404找不到的错误.或者500服务器错误都需要配置相应的页面给用户一个友好的提示,而在Spring Boot中我们需要如何设置. 我们需要实现ErrorController接口,重写h ...
- Google Chrome实用插件
Tempermonkey 打开上面的链接需要KXSW_VPN_FQ,下面提供国内获取方式 下载 Tempermonkey_4.7 [提取码:7019]并解压 打开C:\Users\%USERNAME% ...
- 查看CPU和内存,用机器指令和汇编指令编程【Debug模式】
命令 作用 举例 R 查看,改变CPU寄存器的内容 查看:r 改写:r ax D 查看内存中的内容 d 1000:0 f E 改写内存中的内容 e 1000:0 f U 将内存中的机器指令翻译成汇编指 ...
- 使用Ext 创建树
ext使用的是ext3.4.0版本 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> < ...