cf 700e(sam好题,线段树维护right)
代码参考:http://blog.csdn.net/qq_33229466/article/details/79140428
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=;
int n,mx[maxn],ch[maxn][],fa[maxn],last=,id[maxn],tot=;
int dp[maxn],pos[maxn],cnt,b[maxn],c[maxn],root[maxn],ans;
char s[maxn];
struct node{
int l,r;
}tr[maxn*];
void add(int i,int x){
int p,q,np,nq;
p=last;last=np=++tot;mx[np]=mx[p]+;id[np]=i;
for(;!ch[p][x]&&p;p=fa[p])ch[p][x]=np;
if(!p)fa[np]=;
else{
q=ch[p][x];
if(mx[q]==mx[p]+)fa[np]=q;
else{
nq=++tot;mx[nq]=mx[p]+;id[nq]=i;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
fa[nq]=fa[q];
fa[q]=fa[np]=nq;
for(;ch[p][x]==q;p=fa[p])ch[p][x]=nq;
}
}
}
void ins(int &d,int l,int r,int x){
if(!d)d=++cnt;
if(l==r)return;
int mid=l+r>>;
if(x<=mid)ins(tr[d].l,l,mid,x);
else ins(tr[d].r,mid+,r,x);
}
int merge(int x,int y){
if(!x||!y)return x+y;
int d=++cnt;
tr[d].l=merge(tr[x].l,tr[y].l);
tr[d].r=merge(tr[x].r,tr[y].r);
return d;
}
bool query(int d,int l,int r,int x,int y){
if(!d)return ;
if(l==x&&r==y)return ;
int mid=l+r>>;
if(x<=mid&&query(tr[d].l,l,mid,x,min(y,mid)))return ;
if(y>mid&&query(tr[d].r,mid+,r,max(x,mid+),y))return ;
return ;
}
void build(){
for(int i=;i<=tot;++i)b[mx[i]]++;
for(int i=;i<=tot;++i)b[i]+=b[i-];
for(int i=tot;i>=;--i)c[b[mx[i]]--]=i;
for(int i=tot;i>=;--i){
ins(root[c[i]],,n,id[c[i]]);
root[fa[c[i]]]=merge(root[fa[c[i]]],root[c[i]]);
}
}
int main(){
cin>>n;
scanf("%s",s+);
for(int i=;i<=n;++i)add(i,s[i]-'a');
build();
ans=;
for(int i=;i<=tot;++i){
int x=c[i];
if(fa[x]==)dp[x]=,pos[x]=x;
else if(query(root[pos[fa[x]]],,n,id[x]-mx[x]+mx[pos[fa[x]]],id[x]-)){dp[x]=dp[fa[x]]+;pos[x]=x;}
else {dp[x]=dp[fa[x]];pos[x]=pos[fa[x]];}
ans=max(ans,dp[x]);
}
cout<<ans;
return ;
}
cf 700e(sam好题,线段树维护right)的更多相关文章
- CF - 1108 E 枚举上界+线段树维护
题目传送门 枚举每个点作为最大值的那个点.然后既然是作为最大值出现的话,那么这个点就是不需要被减去的,因为如果最小值也在这个区间内的话,2者都减去1,对答案没有影响,如果是最小值不出现在这个区间内的话 ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
- Codeforces 750E - New Year and Old Subsequence(线段树维护矩阵乘法,板子题)
Codeforces 题目传送门 & 洛谷题目传送门 u1s1 我做这道 *2600 的动力是 wjz 出了道这个套路的题,而我连起码的思路都没有,wtcl/kk 首先考虑怎样对某个固定的串计 ...
- 洛谷 P7879 -「SWTR-07」How to AK NOI?(后缀自动机+线段树维护矩乘)
洛谷题面传送门 orz 一发出题人(话说我 AC 这道题的时候,出题人好像就坐在我的右侧呢/cy/cy) 考虑一个很 naive 的 DP,\(dp_i\) 表示 \([l,i]\) 之间的字符串是否 ...
- [BZOJ 1018] [SHOI2008] 堵塞的交通traffic 【线段树维护联通性】
题目链接:BZOJ - 1018 题目分析 这道题就说明了刷题少,比赛就容易跪..SDOI Round1 Day2 T3 就是与这道题类似的..然而我并没有做过这道题.. 这道题是线段树维护联通性的经 ...
- 洛谷P4243/bzoj1558 [JSOI2009]等差数列(线段树维护差分+爆炸恶心的合并)
题面 首先感谢这篇题解,是思路来源 看到等差数列,就会想到差分,又有区间加,很容易想到线段树维护差分.再注意点细节,\(A\)操作完美解决 然后就是爆炸恶心的\(B\)操作,之前看一堆题解的解释都不怎 ...
- [动态dp]线段树维护转移矩阵
背景:czy上课讲了新知识,从未见到过,总结一下. 所谓动态dp,是在动态规划的基础上,需要维护一些修改操作的算法. 这类题目分为如下三个步骤:(都是对于常系数齐次递推问题) 1先不考虑修改,不考虑区 ...
- bzoj 4184: shallot (线段树维护线性基)
题面 \(solution:\) 这一题绝对算的上是一道经典的例题,它向我们诠释了一种新的线段树维护方式(神犇可以跳过了).像这一类需要加入又需要维护删除的问题,我们曾经是遇到过的像莫对,线段树... ...
- FJUT3568 中二病也要敲代码(线段树维护区间连续最值)题解
题意:有一个环,有1~N编号,m次操作,将a位置的值改为b,问你这个环当前最小连续和多少(不能全取也不能不取) 思路:用线段树维护一个区间最值连续和.我们设出两个变量Lmin,Rmin,Mmin表示区 ...
随机推荐
- asp.net 微信JsSDK
有时间再整理吧 using System; using System.Collections.Generic; using System.Linq; using System.Web; using S ...
- a.vim 插件
引用自 https://blog.csdn.net/zhangsming/article/details/42652695 vim插件之快速切换头(.h)/源(.c,.cpp,.cc)文件——a.v ...
- Mybatis-spring 动态代理
1.UserMapper.java package com.cn.mapper; import java.util.List; import com.cn.pojo.User; public inte ...
- python3 re.compile中含有变量
id = '7F' reg = re.compile(id + '[^\dA-F]?\d') line = ‘122s 7f 3' match = reg.search(line) 在程序中有时候会遇 ...
- gopath环境变量设置
#在国内镜像下载二进制包 wget -c http://www.golangtc.com/static/go/go1.4.1.linux-amd64.tar.gz tar -C /usr/local ...
- CentOS 7 无法yum安装解决方法
1)下载repo文件 wget http://mirrors.aliyun.com/repo/Centos-7.repo 2)备份并替换系统的repo文件 .repo /etc/yum.repos.d ...
- The history of programming languages.(transshipment) + Personal understanding and prediction
To finish this week's homework that introduce the history of programming languages , I surf the inte ...
- ArcGIS自定义工具箱-字段值部分替换
ArcGIS自定义工具箱-字段值部分替换 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:替换某个字段中的字符串 用例:湖南省长沙市=>湖南/长沙:临湘县 ...
- python,关于用户登录与注册问题
tag=Truecount=1while tag: name = input('请输入用户名>>:').strip() password = input('请输入密码>>:') ...
- TCP/UDP 常用端口列表
计算机之间依照互联网传输层TCP/IP协议不同的协议通信,都有不同的对应端口.所以,利用短信(datagram)的UDP,所采用的端口号码不一定和采用TCP的端口号码一样.以下为两种通信协议的端口列表 ...