题意是求一个字符串每个长度的子串出现次数最多的那个出现了多少次,也就是求每个到根的最长路的right集合最大值 。

先建后缀自动机,然后将每个前缀所在的集合的初值设为1,因为所有前缀的right集合肯定不相同,而且它们包含了所有位置。

接下来按到根的最长距离从大到小排序,将right集合累加到parent上。这么排序是因为到根的最长距离长的状态肯定不是到根的最长距离短的状态的parent。

最后直接求到根的不同的最长距离的最大的right集合就行。

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define maxn 250010
using namespace std;
int ans,len,p;
int read()
{
int f=,x=;char ch=getchar();
while(isdigit(ch)== && ch!='-')ch=getchar();
if(ch=='-')f=-,ch=getchar();
while(isdigit(ch))x=x*+ch-'',ch=getchar();
return x*f;
}
void write(int x)
{
int ff=;char ch[];
while(x)ch[++ff]=(x%)+'',x/=;
if(ff==)putchar('');
while(ff)putchar(ch[ff--]);
putchar('\n');
}
typedef struct node
{
int to[],dis,fa;
}spot;
//求每个dis的right集合最大值
//不是每个状态的right集合!!
//ass♂we♂can♂
struct SAM
{
spot x[maxn<<];
int ls,ls2,q,cnt,rt,lst,c[maxn<<],ord[maxn<<],ans[maxn<<],maxl[maxn<<];
int right[maxn<<],f[maxn<<];
char s[maxn];
inline void start()
{
lst=rt=++cnt;
scanf("%s",s+);
ls=strlen(s+);
for(int i=;i<=ls;i++)
extend(i);
}
inline void extend(int pos)
{
int val=s[pos]-'a',p=lst,np=++cnt;
lst=np,x[np].dis=pos;
for(;p&&x[p].to[val]==;p=x[p].fa)x[p].to[val]=np;
if(p==)x[np].fa=rt;
else
{
int q=x[p].to[val];
if(x[q].dis==x[p].dis+)x[np].fa=q;
else
{
int nq=++cnt;
x[nq].dis=x[p].dis+;
memcpy(x[nq].to,x[q].to,sizeof(x[q].to));
x[nq].fa=x[q].fa,x[np].fa=x[q].fa=nq;
for(;x[p].to[val]==q;p=x[p].fa)x[p].to[val]=nq;
}
}
}
inline void qsort()
{
for(int i=;i<=cnt;i++)
c[x[i].dis]++;
for(int i=;i<=ls;i++)
c[i]+=c[i-];
for(int i=;i<=cnt;i++)
ord[c[x[i].dis]--]=i;
}
inline void work()
{
for(int i=,p=rt;i<=ls;i++)
p=x[p].to[s[i]-'a'],right[p]=;
for(int i=cnt;i>=;i--){int u=ord[i];right[x[u].fa]+=right[u];}
for(int i=;i<=cnt;i++)f[x[i].dis]=max(f[x[i].dis],right[i]);
for(int i=ls;i>=;i--)f[i]=max(f[i],f[i+]);
for(int i=;i<=ls;i++)write(f[i]);
// cout<<"He doesn't have hands->"<<endl;
}
}t;
int main()
{
t.start();
t.qsort();
t.work();
return ;
}

并不对劲的SAM

听某位大佬说后缀自动机能解决所有不是字符串dp的字符串题。

并不对劲的spoj nsubstr的更多相关文章

  1. SPOJ - NSUBSTR 后缀自动机板子

    SPOJ - NSUBSTR #include<bits/stdc++.h> #define LL long long #define fi first #define se second ...

  2. SPOJ NSUBSTR (后缀自动机)

    SPOJ NSUBSTR Problem : 给一个长度为n的字符串,要求分别输出长度为1~n的子串的最多出现次数. Solution :首先对字符串建立后缀自动机,在根据fail指针建立出后缀树,对 ...

  3. 【spoj NSUBSTR】 Substrings

    http://www.spoj.com/problems/NSUBSTR/ (题目链接) 题意 给出一个字符串S,令${F(x)}$表示S的所有长度为x的子串出现次数的最大值.求${F(1)..... ...

  4. SPOJ NSUBSTR Substrings 后缀自动机

    人生第一道后缀自动机,总是值得纪念的嘛.. 后缀自动机学了很久很久,先是看CJL的论文,看懂了很多概念,关于right集,关于pre,关于自动机的术语,关于为什么它是线性的结点,线性的连边.许多铺垫的 ...

  5. SPOJ NSUBSTR

    You are given a string S which consists of 250000 lowercase latin letters at most. We define F(x) as ...

  6. SPOJ NSUBSTR Substrings

    题意 dt { font-weight: bold; margin-top: 20px; padding-left: 35px; } dd { box-shadow: 3px 3px 6px #888 ...

  7. SPOJ - NSUBSTR(长度为1-len的字串出现的最大次数

    题意:给你一个字符串,要你输出1-len的字串出现的最大次数. /** @xigua */ #include <stdio.h> #include <cmath> #inclu ...

  8. 【SPOJ -NSUBSTR】Substrings 【后缀自动机+dp】

    题意 给出一个字符串,要你找出所有长度的子串分别的最多出现次数. 分析 我们建出后缀自动机,然后预处理出每个状态的cnt,cnt[u]指的是u这个状态的right集合大小.我们设f[len]为长度为l ...

  9. SPOJ NSUBSTR Substrings ——后缀自动机

    建后缀自动机 然后统计次数,只需要算出right集合的大小即可, 然后更新f[l[i]]和rit[i]取个max 然后根据rit集合短的一定包含长的的性质,从后往前更新一遍即可 #include &l ...

随机推荐

  1. CentOS7安装Tomcat9并设置开机启动

    1.下载 Tomcat 9 CentOS 7 下创建目录并下载文件: cd /usr/local/ mkdir tomcat cd tomcat wget http://mirrors.hust.ed ...

  2. Mongo索引学习笔记

    索引使用场景 优:加快查询速度 劣:增删改会产生额外的开销.占用空间 tips: 返回集合中一半以上的数据,全表扫描的效率高 索引基础 基础操作 查看索引:db.test.getIndexes() 创 ...

  3. 21Spring重用切点表达式

    直接看代码: package com.cn.spring.aop.impl; //加减乘除的接口类 public interface ArithmeticCalculator { int add(in ...

  4. jupyter 教程

    官网: http://jupyter.org/

  5. LeetCode(6) ZigZag Conversion

    题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...

  6. Apache Ambari 2.7.3.0 离线安装

    1. 准备 (内存 3G 硬盘 40G) 0)设置ssh无密码 ssh-keygencat id_rsa.pub >> authorized_keyschmod 700 ~/.sshchm ...

  7. 九度oj 题目1056:最大公约数

    题目1056:最大公约数 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8068 解决:5317 题目描述: 输入两个正整数,求其最大公约数. 输入: 测试数据有多组,每组输入两个正整数. ...

  8. Linux下汇编语言学习笔记46 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  9. 289. Game of Live

    According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...

  10. [bzoj2506]calc_分块处理

    calc bzoj-2506 题目大意:给一个长度为n的非负整数序列A1,A2,…,An.现有m个询问,每次询问给出l,r,p,k,问满足l<=i<=r且Ai mod p = k的值i的个 ...