【SPOJ】Substrings
出现次数很好处理,就是 \(right/endpos\) 集合的大小
那么,直接构建 \(SAM\)
求出每个位置的\(right\)集合大小
直接更新每个节点的\(longest\)就行了
最后短的可以由长的更新过来就好
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
#define RG register
#define MAX 2001000
inline int read()
{
RG int x=0,t=1;RG char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
char ch[MAX];
struct Node
{
int son[26];
int ff,len;
}t[MAX<<1];
int size[MAX];
int tot=1,last=1,c[MAX],a[MAX],ans[MAX];
void extend(int c)
{
int p=last,np=++tot;last=np;
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;
}
}
size[np]=1;
}
int main()
{
scanf("%s",ch+1);
int l=strlen(ch+1);
for(int i=1;i<=l;++i)extend(ch[i]-97);
for(int i=1;i<=tot;++i)c[t[i].len]++;
for(int i=1;i<=tot;++i)c[i]+=c[i-1];
for(int i=1;i<=tot;++i)a[c[t[i].len]--]=i;
for(int i=tot;i;--i)
{
int u=a[i];
size[t[u].ff]+=size[u];
ans[t[u].len]=max(ans[t[u].len],size[u]);
}
for(int i=l-1;i;--i)ans[i]=max(ans[i],ans[i+1]);
for(int i=1;i<=l;++i)printf("%d\n",ans[i]);
return 0;
}
【SPOJ】Substrings的更多相关文章
- 【SPOJ】Substrings(后缀自动机)
[SPOJ]Substrings(后缀自动机) 题面 Vjudge 题意:给定一个长度为\(len\)的串,求出长度为1~len的子串中,出现最多的出现了多少次 题解 出现次数很好处理,就是\(rig ...
- 【SPOJ】Distinct Substrings(后缀自动机)
[SPOJ]Distinct Substrings(后缀自动机) 题面 Vjudge 题意:求一个串的不同子串的数量 题解 对于这个串构建后缀自动机之后 我们知道每个串出现的次数就是\(right/e ...
- 【SPOJ】Distinct Substrings/New Distinct Substrings(后缀数组)
[SPOJ]Distinct Substrings/New Distinct Substrings(后缀数组) 题面 Vjudge1 Vjudge2 题解 要求的是串的不同的子串个数 两道一模一样的题 ...
- 【SPOJ】Distinct Substrings
[SPOJ]Distinct Substrings 求不同子串数量 统计每个点有效的字符串数量(第一次出现的) \(\sum\limits_{now=1}^{nod}now.longest-paren ...
- 【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)
[SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. ...
- 【SPOJ】Longest Common Substring II (后缀自动机)
[SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...
- 【SPOJ】Longest Common Substring(后缀自动机)
[SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...
- 【SPOJ】Power Modulo Inverted(拓展BSGS)
[SPOJ]Power Modulo Inverted(拓展BSGS) 题面 洛谷 求最小的\(y\) 满足 \[k\equiv x^y(mod\ z)\] 题解 拓展\(BSGS\)模板题 #inc ...
- 【SPOJ】QTREE7(Link-Cut Tree)
[SPOJ]QTREE7(Link-Cut Tree) 题面 洛谷 Vjudge 题解 和QTREE6的本质是一样的:维护同色联通块 那么,QTREE6同理,对于两种颜色分别维护一棵\(LCT\) 每 ...
随机推荐
- X-Forward-For ip
用 Firefox 的Moify Headers 插件 服务器重新配置X-Forward-For 为正确的值. 如对典型的nginx + php fastcgi 环境( nginx 与 php fas ...
- CodeForces 547E:Mike and Friends(AC自动机+DFS序+主席树)
What-The-Fatherland is a strange country! All phone numbers there are strings consisting of lowercas ...
- P1955 [NOI2015]程序自动分析[离散化+并查集]
大水题一道,不明白为什么你谷评了个蓝.一看就是离散化,先去满足相等的条件,相等即为两点联通,或者说在同一个集合内.再看不相等,只有两元素在同一集合才不满足.裸的disjoint-set直接上,常数巨大 ...
- ubuntu svn 常用命令
1.svn svn update 更新 新增文件或文件夹并提交svn add "sss" test.py testw.pysvn add "dir" dir_p ...
- [转载]Windows网络编程系列教程之四:Select模型
原文:http://www.51see.com/asp/bbs/public/bp_show.asp?t_id=200308131152297103 讲一下套接字模式和套接字I/O模型的区别.先说明一 ...
- U盘安装CentOS的坑
坑一:U盘安装盘配置的路径错误 采用UltraISO制作的CentOS7的安装光盘,但是在实体机安装的时候,发生了一些奇怪的失败,比如 dracut-initqueue timeout等,后来在网上搜 ...
- IHE-PIX 备注
IHE给出了各个Actor之间如何通讯的建议: 1. 应用程序通讯时必须用MLLP包装或者解析. 2. 客户端建立连接后,服务器端必须用此连接进行应答.客户端可以继续用此连接启 ...
- 获取CPU ID ,disk ID, MAC ID (windows ARM linux Mac)
windows 命令行获取CPU ID,可以用ShellExecute wmic cpu get processorid ProcessorId BFEBFBFF000506E3 开源库: 查询CPU ...
- 如何更改linux文件的拥有者及用户…
本文整理自: http://blog.163.com/yanenshun@126/blog/static/128388169201203011157308/ http://ydlmlh.iteye.c ...
- 关于 Number() parsint() abs() 的区别
1. parseInt(‘’) parseInt() 函数可解析一个字符串,并返回一个整数. 如果第一个字符不是数字或者负号,parseInt() 就会返回NaN 2.Number() ...