【hihocoder】sam-3
把Parent Tree拓扑排序下,然后从下往上合并。
具体的看官方题解啦~
#include<bits/stdc++.h>
#define N 1000010
using namespace std;
int n;char s[N];
int tot=,head[N<<];
struct Edge{int u,v,next;}G[N<<];
int size[N<<],ans[N];
struct Suffix_Automaton{
int fa[N<<],ch[N<<][],l[N<<],r[N<<],rt,cnt,last;
Suffix_Automaton(){cnt=last=;rt=;}
void ins(int c){
int p=last,np=++cnt;last=np;l[np]=l[p]+;
for(;p&&!ch[p][c];p=fa[p])ch[p][c]=np;
if(!p){fa[np]=rt;r[np]=;}
else{
int q=ch[p][c];
if(l[p]+==l[q]){fa[np]=q;r[np]=l[q]+;}
else{
int nq=++cnt;l[nq]=l[p]+;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
fa[nq]=fa[q];r[nq]=l[fa[q]]+;
fa[q]=fa[np]=nq;r[q]=r[np]=l[nq]+;
for(;p&&ch[p][c]==q;p=fa[p])ch[p][c]=nq;
}
}
}
}sam;
inline void addedge(int u,int v){
G[++tot].u=u;G[tot].v=v;G[tot].next=head[u];head[u]=tot;
}
void dfs(int u){
for(int i=head[u];i;i=G[i].next){
int v=G[i].v;dfs(v);
size[u]+=size[v];
}
if(!head[u])size[u]=;
}
int main(){
scanf("%s",s+);n=strlen(s+);
sam.ins();
for(int i=;i<=n;i++)sam.ins(s[i]-'a'+);
for(int i=;i<=sam.cnt;i++)addedge(sam.fa[i],i);dfs();
for(int i=;i<=sam.cnt;i++)ans[sam.l[i]]=max(ans[sam.l[i]],size[i]);
for(int i=n-;i;i--)ans[i]=max(ans[i],ans[i+]);
for(int i=;i<=n;i++)printf("%d\n",ans[i]);
}
【hihocoder】sam-3的更多相关文章
- 【hihoCoder】1148:2月29日
问题:http://hihocoder.com/problemset/problem/1148 给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期). 思路: 1. 将问题转换成求两个日 ...
- 【hihoCoder】1288 : Font Size
题目:http://hihocoder.com/problemset/problem/1288 手机屏幕大小为 W(宽) * H(长),一篇文章有N段,每段有ai个字,要求使得该文章占用的页数不超过P ...
- 【hihoCoder】1082: 然而沼跃鱼早就看穿了一切
题目:http://hihocoder.com/problemset/problem/1082 输入一个字符串,将其中特定的单词替换成另一个单词 代码注意点: 1. getline(istre ...
- 【hihoCoder】1121:二分图一·二分图判定
题目 http://hihocoder.com/problemset/problem/1121 无向图上有N个点,两两之间可以有连线,共有M条连线. 如果对所有点进行涂色(白/黑),判定是否存 ...
- 【hihoCoder】1036 Trie图
题目:http://hihocoder.com/problemset/problem/1036 给一个词典dict,词典中包含了一些单词words.要求判断给定的一个文本串text中是否包含这个字典中 ...
- 【hihoCoder】1039 : 字符消除
题目:http://hihocoder.com/problemset/problem/1039 给定一个字符串s,只包含'A', 'B', 'C'三种字符 1. 向 s 的任意位置 (包括头和尾) 中 ...
- 【hihoCoder】1037 : 数字三角形
题目:http://hihocoder.com/problemset/problem/1037 一个迷宫有n层,第 i 层有 i 个房间 从第i层的第i个房间(i, i)可以走到第i+1层的第i个房间 ...
- 【hihoCoder】1033: 交错和
初探数位dp 介绍了数位类统计的基础知识.以下列出其中的基础点: 基本问题 统计在区间[l, r]中满足条件的数的个数 思路 1. [l, r] 将问题转换为 在[0, r]中满足条件的个数 - 在[ ...
- 【Hihocoder】1014 : Trie树
问题:http://hihocoder.com/problemset/problem/1014 给定一个字符串字典dict,输入字符串str, 要求从dict中找出所有以str为前缀的字符串个数. 构 ...
- 【hihoCoder】1049.后序遍历
问题:http://hihocoder.com/problemset/problem/1049?sid=767510 已知一棵二叉树的前序遍历及中序遍历结果,求后序遍历结果 思路: 前序:根-左子树- ...
随机推荐
- Git更新github项目
1. 把github上你想要更新修改的项目克隆到本地 $ git clone https://github.com/delav/test.git 2. 根据自己需求对项目进行修改 3. 把项目放到缓存 ...
- [POI2007]ZAP-Queries && [HAOI2011]Problem b 莫比乌斯反演
1,[POI2007]ZAP-Queries ---题面---题解: 首先列出式子:$$ans = \sum_{i = 1}^{n}\sum_{j = 1}^{m}[gcd(i, j) == d]$$ ...
- Redis 的安装配置介绍
redis 是一个高性能的key-value数据库. redis的出现,很大程度补偿了memcached这类keyvalue存储的不足,在部 分场合可以对关系数据库起到很好的补充作用.它提供了Pyth ...
- 如何用好 github 中的 watch、star、fork
http://www.jianshu.com/p/6c366b53ea41 https://www.zhihu.com/question/20431718 在每个 github 项目的右上角,都有三个 ...
- selenium - webdriver常用方法
先定位元素,定位元素后,需要对元素进行后续操作,单击按钮/输入文本,等等. from selenium import webdriver driver = webdriver.Chrome() dri ...
- bzoj 1468 Tree 点分
Tree Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1972 Solved: 1101[Submit][Status][Discuss] Desc ...
- arm开发板刷机方法
1.linux系统启动方式 bootloader->kernel->system 在嵌入式系统中内存为DRAM,inand flash 都不能直接启动需要被初始化.其中初始化程序在(boo ...
- Nginx的启动、停止、平滑重启
转载自:http://www.xj123.info/2572.html 启动Nginx /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/ngi ...
- devDependencies 和 dependencise 的区别
在使用 npm install 安装模块或者插件的时候,在命令中可以添加俩种参数把他们写入到 pakeage.json 文件里面去: --save ( 简写 -S ) --save-dev ( 简写 ...
- ICPC2017 Urumqi - K - Sum of the Line
题目描述 Consider a triangle of integers, denoted by T. The value at (r, c) is denoted by Tr,c , where 1 ...