构建后缀自动机,求出后缀树

比较明显的dp

设 \(f[i]\) 表示从上而下到达当前点能够满足条件的最优值

只需要检查父亲节点是否在当前串中出现过两次就行了

这个判断用 \(endpos\) 来判断

如果出现过超过两次,那么在当前点所掌控的任意一个 \(endpos\) 以及前面的区间中

必定出现了超过两次

用一个线段树合并求 \(endpos\) 集合

然后计算一下出现次数就好了

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define MAX 444444
char ch[MAX];
int n,last=1,tot=1,TOT;
struct SegNode{int ls,rs;}st[MAX*25];
int rt[MAX],f[MAX],ans=1;
void Modify(int &x,int l,int r,int p)
{
if(!x)x=++TOT;if(l==r)return;
int mid=(l+r)>>1;
if(p<=mid)Modify(st[x].ls,l,mid,p);
else Modify(st[x].rs,mid+1,r,p);
}
int Merge(int x,int y)
{
if(!x||!y)return x|y;
int z=++TOT;
st[z].ls=Merge(st[x].ls,st[y].ls);
st[z].rs=Merge(st[x].rs,st[y].rs);
return z;
}
int Query(int x,int l,int r,int L,int R)
{
if(!x)return 0;if(L<=l&&r<=R)return 1;
int mid=(l+r)>>1;
if(L<=mid&&Query(st[x].ls,l,mid,L,R))return 1;
if(R>mid&&Query(st[x].rs,mid+1,r,L,R))return 1;
return 0;
}
struct Node{int ff,len,pos,son[26];}t[MAX];
void extend(int c,int pos)
{
int p=last,np=++tot;last=np;t[np].pos=pos;
t[np].len=t[p].len+1;
while(p&&!t[p].son[c])t[p].son[c]=np,p=t[p].ff;
if(!p)t[np].ff=1;
else
{
int q=t[p].son[c];
if(t[q].len==t[p].len+1)t[np].ff=q;
else
{
int nq=++tot;
t[nq]=t[q];t[nq].len=t[p].len+1;
t[q].ff=t[np].ff=nq;
while(p&&t[p].son[c]==q)t[p].son[c]=nq,p=t[p].ff;
}
}
}
int a[MAX],p[MAX],top[MAX];
int main()
{
scanf("%d",&n);scanf("%s",ch+1);
for(int i=1;i<=n;++i)extend(ch[i]-97,i),Modify(rt[last],1,n,i);
for(int i=1;i<=tot;++i)a[t[i].len]++;
for(int i=1;i<=n;++i)a[i]+=a[i-1];
for(int i=tot;i>=1;--i)p[a[t[i].len]--]=i;
for(int i=tot;i>1;--i)rt[t[p[i]].ff]=Merge(rt[t[p[i]].ff],rt[p[i]]);
for(int i=2;i<=tot;++i)
{
int u=p[i],fa=t[u].ff;
if(fa==1){f[u]=1,top[u]=u;continue;}
int x=Query(rt[top[fa]],1,n,t[u].pos-t[u].len+t[top[fa]].len,t[u].pos-1);
if(x)f[u]=f[fa]+1,top[u]=u;
else f[u]=f[fa],top[u]=top[fa];
ans=max(ans,f[u]);
}
printf("%d\n",ans);
return 0;
}

CF 700E的更多相关文章

  1. [LOJ 6288]猫咪[CF 700E]Cool Slogans

    [LOJ 6288]猫咪[CF 700E]Cool Slogans 题意 给定一个字符串 \(T\), 求一个最大的 \(K\) 使得存在 \(S_1,S_2,\dots,S_k\) 满足 \(S_1 ...

  2. cf 700e(sam好题,线段树维护right)

    代码参考:http://blog.csdn.net/qq_33229466/article/details/79140428 #include<iostream> #include< ...

  3. ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'

    凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...

  4. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  5. cf Round 613

    A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...

  6. ARC下OC对象和CF对象之间的桥接(bridge)

    在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...

  7. [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现

    1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...

  8. CF memsql Start[c]UP 2.0 A

    CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...

  9. CF memsql Start[c]UP 2.0 B

    CF memsql Start[c]UP 2.0 B B. Distributed Join time limit per test 1 second memory limit per test 25 ...

随机推荐

  1. int型变量,不使用中间变量完成互换

    package com.t_02; /** * 定义两个int类型的数,完成交换,不使用第三方变量 * @author Administrator * */ public class t1 { pub ...

  2. Linux网络编程socket错误分析

    socket错误码: EINTR: 阻塞的操作被取消阻塞的调用打断.如设置了发送接收超时,就会遇到这种错误. 只能针对阻塞模式的socket.读,写阻塞的socket时,-1返回,错误号为INTR.另 ...

  3. GSM —— 商业蜂窝通信系统

    用户漫游: HLR:当用户办卡时,当地运营商把用户资料(归属地信息等)输入 HLR: VLR:当用户漫游到别的城市时,漫游地的 VLR 把用户资料从 HLR 复制过来,用户便可以继续享受运营商的通信服 ...

  4. poj3784Running Median——堆维护中间值

    题目:http://poj.org/problem?id=3784 将较小的数放入大根堆,较大的数放入小根堆,控制较小数堆大小比较大数堆小1,则较大数堆堆顶即为中位数. 代码如下: #include& ...

  5. DataGrid当列宽超出当前宽度时,没有数据也恒有滚动条

    附件是DataGrid支持滚动条的文件. 具体使用如下: 1)DataGrid使用控件模板 <Setter Property="Template" Value="{ ...

  6. deleteMany is not a function

    问题: 同事使用了deleteMany方法用于删除数据,但是全公司只有我一个人报错deleteMany is not a function. 很自然,输出了model.deleteMany,得到的结果 ...

  7. Robot Framework基础学习(六)

    网页弹窗 参考:http://blog.csdn.net/Allan_shore_ma/article/details/65629246 常见的网页弹窗,有如下几类: alert ajax ifram ...

  8. iview组件select之默认展示label,并传空value做方法入参

    要求: 默认查询操作日期在当日的数据:(打开页面时默认选中时间.全部) 后台约定:选定“全部”这个条件,传的值是空"" 综上:使用select选择框的v-model绑定数据,使用: ...

  9. poj3535 A+B (大数加法)

    A+B Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 811   Accepted: 371 Description The ...

  10. location对象介绍

    Location 对象 Location 对象 Location 对象包含有关当前 URL 的信息. Location 对象是 Window 对象的一个部分,可通过 window.location 属 ...