【暂咕咕咕】SuffixTree
#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e6+10;
typedef long long ll;
char s[MAXN];
int n,siz[MAXN<<1];
ll ans=0;
const int inf=1e8;
struct SuffixTree{
int link[MAXN<<1],len[MAXN<<1],start[MAXN<<1],s[MAXN<<1];
int n,tail,now,rem,ch[MAXN<<1][27];
SuffixTree(){tail=1,n=0,rem=0,now=1,len[0]=inf;}
int newnode(int st,int le){
link[++tail]=1;start[tail]=st;len[tail]=le;return tail;
/*build new node
link->1,start=st(have been known)
len->le have been known
tail is the new number*/
}
void Extend(int x){
s[++n]=x;rem++;
/*add len(n),add rem
n->all strings' number
rem->now length(not the waited suffixstrings
suffixstrings is showed by "rem" & "now"
start[u]&len[u]:
is meaning of u's father is s[start[u]->start[u]+len[u]-1]*/
for(int last=1;rem;){
while(rem>len[ch[now][s[n-rem+1]]])
rem-=len[now=ch[now][s[n-rem+1]]];
/*
find the edge which the head is s[n-rem+1]
because that len(rem) may bigger than this edge's len
so we should del it's len and jump to the next
this is the setp2.
*/
int &v=ch[now][s[n-rem+1]];int c=s[start[v]+rem-1];
/*
v->the edge's last point
c->this edge's last character
*/
if(!v||x==c){
link[last]=now;last=now;
if(!v)v=newnode(n,inf);
/*
if there is no edge which is follow the rule
or,2 step is failed
then we should add the "suffix link"->link[last]=now,last=now
*/
else break;
/* if we find this edge is being,also
the last character have been there
it's meaning that we needn't to add now's character
so,we should break.
*/
}
else{
// if the last character is not being
int u=newnode(start[v],rem-1);
// build a new node,and split this edge
ch[u][c]=v;ch[u][x]=newnode(n,inf);
/*
u->have two points:one is v->inherit the before
and the other one is x:insert x.
*/
start[v]+=rem-1;len[v]-=rem-1;
/*
also,because we split this road
v's start and len should be updated
start+=rem-1,also the len[v] should cut rem-1
*/
link[last]=v=u;last=u;
//build lead to update the suffix link.
}
if(now==1)rem--;
/*
root is 1
if now=1,rem should be less
*/
else now=link[now];
/*
else,in order to add the next character,we should jump to the now's suffix link.
the meaning of suffix link is making find the next right edge and updating it more convenient and faster.
*/
}
}
}sft;
int dfs(int u,int dep){
if(dep>=inf)return 1;
siz[u]=0;
for(int i=0;i<=26;++i){
if(sft.ch[u][i]){
/*
if we can jump this edge
then,we continue to dfs and update the siz
*/
int d=dfs(sft.ch[u][i],dep+sft.len[sft.ch[u][i]]);
siz[u]+=d;
}
}
if(siz[u]>=2)ans=max(ans,1LL*siz[u]*dep);//the problem's querying
return siz[u];
}
int main(){
scanf("%s",s+1);
int n=strlen(s+1);
for(int i=1;i<=n;++i)s[i]-='a',sft.Extend(s[i]);//Extend the string
sft.Extend(26);//End character
dfs(1,0);//add up the answer
printf("%lld\n",ans);
}
\(\text{The code has explained by English.And the explaining will be updated after the High school entrance examination}\)
【暂咕咕咕】SuffixTree的更多相关文章
- (转)S5pv210 HDMI 接口在 Linux 3.0.8 驱动框架解析 (By liukun321 咕唧咕唧)
作者:liukun321 咕唧咕唧 日期:2014.1.18 转载请标明作者.出处:http://blog.csdn.net/liukun321/article/details/18452663 本文 ...
- u-boot for tiny210 ver1.0(by liukun321咕唧咕唧)
新版本下载: 下面的链接提供了较新版本的源码 ver4.0源码下载:u-boot for tiny210 ver4.0 ver3.1源码下载: u-boot for tiny210 ver3.1 v ...
- 基于S5pv210流媒体server的实现之网络摄像头(by liukun321 咕唧咕唧)
这里仅介绍流媒体server端的实现思路.及编码注意问题,不会贴代码的详细实现. 直接入正题先介绍一下系统硬件框架: server端连接PC机用VLC播放例如以下图: server端应用程序能够分为图 ...
- linux多线程驱动中调用udelay()对整个系统造成的影响(by liukun321咕唧咕唧)
以前没考虑过这个问题,而且之前可能运气比较好,虽然用了udelay但也没出什么奇怪的问题,今天在 CSDN上看到了一篇关于此问题帖子,觉得很受用,再此做简要的记录和分析: 驱动开的是内核线程 跟普通进 ...
- FT5X06 如何应用在10寸电容屏(linux-3.5电容屏驱动简析&移植10寸电容屏驱动到Android4.2) (by liukun321咕唧咕唧)
这是几个月以前的东西了,在彻底遗忘之前拿出来好好写写.做个笔记,也算是造福后来人了.在做这个项目之前,没有做过电容屏的驱动,印象中的电容触摸屏是不需要校正的.IC支持多大的屏就要配多大的屏.但是拿到需 ...
- S5pv210 HDMI 接口在 Linux 3.0.8 驱动框架解析
作者:liukun321 咕唧咕唧 日期:2014.1.18 转载请标明作者.出处:http://blog.csdn.net/liukun321/article/details/18452663 本文 ...
- 聊聊GIS中的坐标系|再版
本文约6500字,建议阅读时间15分钟. 作者:博客园/B站/知乎/csdn/小专栏 @秋意正寒 版权:转载请告知,并在转载文上附上转载声明与原文链接(https://www.cnblogs.com/ ...
- [zt]给你的Mp4大换血,精选Touch里3年收集的900多首歌,"经典不忍去的""最新近流行的",与你共享~~
如果你是音乐爱好者: 这些歌, 请戴上耳机, 调大音量, 一个人听 ,全世界 都是你的!!!!! (一)这些歌很温暖,没有金属味,适合有阳光的午后,很悠闲... [Anaesthesia]Maximi ...
- 关于Yaffs2在u-boot中的支持
开发板是一块2G的MLC的NandFlash,页大小8k+512,为其移植u-boot到yaffs2这了.以前在Mini2440上移植过2k+64的slc的NandFlash的Yaffs2支持,当然也 ...
随机推荐
- Android开发之常用框架WebView详解代码。超详细,送给初学者,完全掌握此控件
这是我特意为新手小白写的一个代码,教大家完完全全掌握WebView, 我感觉,你看懂这个,基本上可以满足以后工作中的需要了,(只针对Webview的使用),但是其实它还有好多功能,比如真正的设计到和H ...
- Oracle的timestamp字段更新实验 结论:只有逐条更新才能保证timestamp字段有差别,批量更新只会得到一致的时间,此操作无关时间精度.
有这么一张表: create table hy_testtime( id number(6,0) not null primary key, name nvarchar2(20) not null, ...
- leetcode刷题-83删除排序链表中的重复元素
题目 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 实现 # Definition for singly-li ...
- 11.QT-ffmpeg+QAudioOutput实现音频播放器
1.前言 由于QAudioOutput支持的输入数据必须是原始数据,所以播放mp3,WAV,AAC等格式文件,需要解封装后才能支持播放. 而在QT中,提供了QMediaPlayer ...
- Django启动框架自带原始页面(Django一)
1.安装,cmd中输入命令: pip install django (前提是python已安装完成,才可以使用pip这个python的库管理工具)ps:在cmd中使用pip命令安装时可能因为速度过慢而 ...
- json模块:json.dumps()、json.loads()、json.dump()、json.load()
json.dumps().json.loads().json.dump().json.load() 4个方法的总结和使用: 注意:存在文件里面的东西,读出来都是字符串 import json 1.js ...
- Docker学习笔记,从原理到实践
什么是docker Docker是使用go语言基于LINUX内核的cgroup,namespace以及AUFS 类的 Union FS 等技术,对进程进行封装隔离的一种操作系统层面的虚拟化技术,由于隔 ...
- Axios源码深度剖析
Axios源码深度剖析 - XHR篇 axios 是一个基于 Promise 的http请求库,可以用在浏览器和node.js中,目前在github上有 42K 的star数 分析axios - 目录 ...
- Java8学习小记
转载自https://segmentfault.com/a/1190000006985405 2014年,Oracle发布了Java8新版本.对于Java来说,这显然是一个具有里程碑意义的版本.尤其是 ...
- hystrix源码之请求缓存
HystrixRequestCache 请求缓存.内部是一个静态ConcurrentHashMap存储各个命令的缓存器,RequestCacheKey为key,HystrixRequestCache为 ...