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

比较明显的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. Strom 消息处理机制 中英对照翻译 (Storm如何保证消息被完全处理)

    官方链接: http://storm.incubator.apache.org/documentation/Guaranteeing-message-processing.html What does ...

  2. (1)Java多线程编程核心——Java多线程技能

    1.为什么要使用多线程?多线程的优点? 提高CPU的利用率 2.什么是多线程? 3.Java实现多线程编程的两种方式? a.继承Thread类 public class MyThread01 exte ...

  3. 理解多线程中的ManualResetEvent(C#)

    线程是程序中的控制流程的封装.你可能已经习惯于写单线程程序,也就是,程序在它们的代码中一次只在一条路中执行.如果你多弄几个线程的话,代码运行可能会更加“同步”.在一个有着多线程的典型进程中,零个或更多 ...

  4. Java如何创建内部类对象

    public static void main(String[] args) { Person p = new Test().new Person(); } class Person { String ...

  5. 关于ng-class中添加多个样式类的解决方案

    想要达到ng-class的效果,有两种写法 1.class=“{{class}} class1 class2” 2.ng-class="{true: 'active', false: 'in ...

  6. javacpp-FFmpeg系列之2:通用拉流解码器,支持视频拉流解码并转换为YUV、BGR24或RGB24等图像像素数据

    javacpp-ffmpeg系列: javacpp-FFmpeg系列之1:视频拉流解码成YUVJ420P,并保存为jpg图片 javacpp-FFmpeg系列之2:通用拉流解码器,支持视频拉流解码并转 ...

  7. 重学JAVA基础(四):线程的创建与执行

    1.继承Thread public class TestThread extends Thread{ public void run(){ System.out.println(Thread.curr ...

  8. 0001_第一个测试小程序Login

    # -*- coding:utf-8 -*- user = raw_input("Username:") password = raw_input("Password:& ...

  9. my emacs configuration

    modified from https://github.com/flyingmachine/emacs-for-clojure ;;;; ;; Packages ;;;; ;; Define pac ...

  10. CodeForces 484A Bits(水题)

    A. Bits time limit per test 1 second memory limit per test 256 megabytes input standard input output ...