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 ...
随机推荐
- Experimental Educational Round: VolBIT Formulas Blitz A
Description The HR manager was disappointed again. The last applicant failed the interview the same ...
- Educational Codeforces Round 7 B
Description You are given the current time in 24-hour format hh:mm. Find and print the time after a ...
- linux学习3(vim)
一.文档编辑 1. vi和vim命令 Vim的打开文件的方式(4种,要求掌握的就前三种): 1. vim 文件路径 ...
- JavaScript中使用ActiveXObject操作本地文件夹的方法
转载地址 http://www.jb51.net/article/48538.htm 在Windows平台上, js可以调用很多Windows提供的ActivexObject,本文就使用js来实 ...
- python模块之beautifulSoup
1. Beautiful Soup的简单介绍 Beautiful Soup是python的一个库,主要的功能是从网页抓取数据,并对数据进行分析.官方解释为:Beautiful Soup提供一些简单的. ...
- 4.centos7 docker 安装
参考这个文档进行安装docker: http://www.runoob.com/docker/centos-docker-install.html 开机启动 systemctl enable dock ...
- matlab的一些关于块分类的函数~~~
1. nlfilter(General sliding-neighborhood operations) B = nlfilter(A, [m n], fun),这是一个其中A是图像[m n]是图像 ...
- Mybatis学习笔记11 - 动态sql之trim标签
trim标签体中是整个字符串拼串后的结果.prefix="" 前缀: prefix给拼串后的整个字符串加一个前缀prefixOverrides="" 前缀覆盖: ...
- 百度 CDN公共库
http://developer.baidu.com/wiki/index.php?title=docs/cplat/libs 简介 CDN公共库是指将常用的JS库存放在CDN节点,以方便广大开发者直 ...
- ETL模型设计
传统的关系数据库一般采用二维数表的形式来表示数据,一个维是行,另一个维是列,行和列的交叉处就是数据元素.关系数据的基础是关系数据库模型,通过标准的SQL语言来加以实现. 数据仓库是多维数据库,它扩展了 ...