1600 Simple KMP

对于一个字符串|S|,我们定义fail[i],表示最大的x使得S[1..x]=S[i-x+1..i],满足(x<i)
显然对于一个字符串,如果我们将每个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取模

 
Input
第一行一个正整数Q
Q<=10^5
第二行一个长度为Q的字符串S
Output
输出Q行,第i行表示前i个字符组成的字符串的答案
Input示例
5
abaab
Output示例
0
0
1
4
9
SkyDec (题目提供者)
 
一开始想错了,于是去写LCT+SAM。然后发现竟然撞上正解了......
想法:首先明白只需统计每次加入的点连到每个后缀的Next树的深度,真正的答案可以求前缀和得到。
然后发现Next树的一个节点的深度,其实就是以该点为结尾的后缀能匹配多少前缀(长度长的可以包含短的,连最长的就构成Next树了)。
于是问题变成了以该点为结尾的后缀在原串中出现多少次,这个可以用SAM的parent树的right以及step求出来。
可以使用LCT+SAM,或者离线后树剖维护一下。
突然发现我的LCT比我的树剖快.....
Code
#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/树链剖分的更多相关文章

  1. 51Nod 1600 Simple KMP 解题报告

    51Nod 1600 Simple KMP 对于一个字符串\(|S|\),我们定义\(fail[i]\),表示最大的\(x\)使得\(S[1..x]=S[i-x+1..i]\),满足\((x<i ...

  2. bryce1010专题训练——LCT&&树链剖分

    LCT&&树链剖分专题 参考: https://blog.csdn.net/forever_wjs/article/details/52116682

  3. 【BZOJ】1036: [ZJOI2008]树的统计Count(lct/树链剖分)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1036 lct: (ps:为嘛我的那么慢T_T,不知道排到哪了..难道别人都是树剖吗...看来有必要学 ...

  4. Cogs 1583. [POJ3237]树的维护 LCT,树链剖分

    题目:http://cojs.tk/cogs/problem/problem.php?pid=1583 1583. [POJ3237]树的维护 ★★★☆   输入文件:maintaintree.in  ...

  5. Cogs 1672. [SPOJ375 QTREE]难存的情缘 LCT,树链剖分,填坑计划

    题目:http://cojs.tk/cogs/problem/problem.php?pid=1672 1672. [SPOJ375 QTREE]难存的情缘 ★★★☆   输入文件:qtree.in  ...

  6. bzoj 4817: [Sdoi2017]树点涂色 LCT+树链剖分+线段树

    题目: Bob有一棵n个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同. 定义一条路径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜色. Bob可能会进 ...

  7. 51nod 1600 Simple KMP【后缀自动机+LCT】【思维好题】*

    Description 对于一个字符串|S|,我们定义fail[i],表示最大的x使得S[1..x]=S[i-x+1..i],满足(x<i) 显然对于一个字符串,如果我们将每个0<=i&l ...

  8. 51nod 1600 Simple KMP

    又被机房神犇肉丝哥哥和glory踩爆了 首先这个答案的输出方式有点套路,当前的答案=上一个答案+每一个后缀的f值=上一个答案+上一次算的每个后缀的f值+当前每个后缀的深度 这个题意给了个根深度为-1有 ...

  9. Bzoj 2243: [SDOI2011]染色 树链剖分,LCT,动态树

    2243: [SDOI2011]染色 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 5020  Solved: 1872[Submit][Status ...

随机推荐

  1. Nginx静态服务,域名解析

    安装这里就不写了在LNMP里有具体的安装 1.1 常用来提供静态Web服务的软件有如下三种:     Apache:这是中小型Web服务的主流,Web服务器中的老大哥.     Nginx:大型网 ...

  2. 导入AppiumLibrary报错: ImportError: cannot import name 'InvalidArgumentException

    导入AppiumLibrary报错: ImportError: cannot import name 'InvalidArgumentException报错原因 selenium.common.exc ...

  3. 谨慎使用多线程中的fork 学习!!!!

    前言 在单核时代,大家所编写的程序都是单进程/单线程程序.随着计算机硬件技术的发展,进入了多核时代后,为了降低响应时间,重复充分利用多核cpu的资源,使用多进程编程的手段逐渐被人们接受和掌握.然而因为 ...

  4. 登陆jq表单验证及jqcookie记住密码实例

    <p><%@ page contentType="text/html; charset=utf-8"%> <%@taglib prefix=" ...

  5. leetcode 182. Duplicate Emails having的用法 SQL执行顺序

    https://leetcode.com/problems/duplicate-emails/description/ 首先sql的执行顺序是 from-->where-->group b ...

  6. DRF之权限认证频率组建

    认证组件 很久很久以前,Web站点只是作为浏览服务器资源(数据)和其他资源的工具,甚少有什么用户交互之类的烦人的事情需要处理,所以,Web站点的开发这根本不关心什么人在什么时候访问了什么资源,不需要记 ...

  7. 小萝卜控机大师录制脚本(手机app自动化)

    手机自动化测试 之前发布过小萝贝控机大师与按键精灵结合实现手机自动化测试的功能,小萝贝控机大师升级了实现了更多手机自动化测试的功能,如下: l 手机功能自动化测试:录制脚本,检查点时点击小萝贝控机大师 ...

  8. 基于Python实现邮件发送

    import smtplibfrom email.mime.text import MIMETextemail_host = 'smtp.163.com' # 邮箱地址email_user = 'sz ...

  9. eclipse启动的时候报错An internal error occurred during: "Initializing Java Tooling"

    eclipse ->windows ->Perspactive -> Reset perspactive 重置视图可以解决

  10. 利用nginx的fastcgi_cache模块来做缓存

    nginx不仅有个大家很熟悉的缓存代理后端内容的proxy_cache,还有个被很多人忽视的fastcgi_cache. proxy_cache的作用是缓存后端服务器的内容,可能是任何内容,包括静态的 ...