后缀自动机+dp。

后缀自动机主要是在functioner大牛那里学习的:http://blog.sina.com.cn/s/blog_70811e1a01014dkz.html

这道题是在No_stop大牛那里学习的:http://blog.csdn.net/no__stop/article/details/11784715

特别感谢这两位大牛!贴上代码作为以后的模板吧。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define CLR(a, b) memset(a, b, sizeof(a)) using namespace std;
const int N = 550000; struct suffix_automaton
{
char s[N];
int chd[N][26],pre[N],step[N];
int last,tot;
inline void rush(int v)
{
step[++tot]=v;
CLR(chd[tot], 0);
}
void Extend(int ch)
{
rush(step[last]+1);
int p=last,np=tot;
for (; !chd[p][ch]; p=pre[p]) chd[p][ch]=np;
if (!p) pre[np]=1;
else
{
int q=chd[p][ch];
if (step[q]!=step[p]+1)
{
rush(step[p]+1);
int nq=tot;
memcpy(chd[nq],chd[q],sizeof(chd[q]));
pre[nq]=pre[q];
pre[q]=pre[np]=nq;
for (; chd[p][ch]==q; p=pre[p]) chd[p][ch]=nq;
} else pre[np]=q;
}
last=np;
}
void Build()
{
scanf("%s", s);
tot = last = 1;
CLR(pre,0);CLR(step,0);CLR(chd[1], 0);
for (int i=0,End=strlen(s); i<End; i++) Extend(s[i]-'a');
}
int pos[N], cnt[N], dp[N], g[N];
void Work()
{
Build();
int len = strlen(s);dp[0] = 0;
for(int i = 0; i <= len; i ++) cnt[i] = 0;///清零
for(int i = 1; i <= tot; i ++) cnt[step[i]] ++;///统计长度为step[i]的节点个数。
for(int i = 1; i <= len; i ++) cnt[i] += cnt[i - 1];///用来hash到(1-tot)。
for(int i = 1; i <= tot; i ++) pos[cnt[step[i]] --] = i, g[i] = dp[i] = 0;///pos表示长度为step[i]的串位置为i。
int p = 1;
for(int i = 0; i < len; i ++) g[p = chd[p][s[i] - 'a']] ++;///顺着串走一遍
for(int i = tot; i > 0; i --)///反着来,确保right集合可以用pre求个数。
{
p = pos[i];///反hash
dp[step[p]] = max(dp[step[p]], g[p]);
g[pre[p]] += g[p];///求right集合元素个数。
}
for(int i = len - 1; i > 0; i --)
dp[i] = max(dp[i], dp[i + 1]);
for(int i = 1; i <= len; i ++)
printf("%d\n", dp[i]);
}
}Suf; int main()
{
Suf.Work();
}

SPOJ 8222. Substrings(后缀自动机模板)的更多相关文章

  1. spoj 8222 Substrings (后缀自动机)

    spoj 8222 Substrings 题意:给一个字符串S,令F(x)表示S的所有长度为x的子串中,出现次数的最大值.求F(1)..F(Length(S)) 解题思路:我们构造S的SAM,那么对于 ...

  2. SPOJ NSUBSTR Substrings 后缀自动机

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

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

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

  4. spoj 1812 lcsII (后缀自动机)

    spoj 1812 lcsII (后缀自动机) 题意:求多个串的lcs,最多10个串,每个串最长10w 解题思路:后缀自动机.先建好第一个串的sam,然后后面的串拿上去跑(这个过程同前一题).sam上 ...

  5. 牛客网 桂林电子科技大学第三届ACM程序设计竞赛 A.串串-后缀自动机模板题

    链接:https://ac.nowcoder.com/acm/contest/558/A来源:牛客网 A.串串 小猫在研究字符串. 小猫在研究字串. 给定一个长度为N的字符串S,问所有它的子串Sl…r ...

  6. hdu4622(后缀自动机模板)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意: 先输入一个长度为 n 的字符串, 然后有 q 个形如 l, r 的询问, 对于每个询问 ...

  7. ●SPOJ 8222 NSUBSTR–Substrings(后缀自动机)

    题链: http://www.spoj.com/problems/NSUBSTR/ 题解: 后缀自动机的水好深啊!懂不了相关证明,带着结论把这个题做了.看来这滩深水要以后再来了. 本题要用到一个叫 R ...

  8. Substrings SPOJ - NSUBSTR (后缀自动机)

    Substrings \[ Time Limit: 100ms\quad Memory Limit: 1572864 kB \] 题意 给出一个长度为 \(250000\) 的字符串,求出所有 \(x ...

  9. spoj - Longest Common Substring(后缀自动机模板题)

    Longest Common Substring 题意 求两个串的最长公共子串. 分析 第一个串建后缀自动机,第二个串在自动机上跑,对于自动机上的结点(状态)而言,它所代表的最大长度为根结点到当前结点 ...

随机推荐

  1. Django的视图层

    HttpResquest对象: request属性: /* 1.HttpRequest.GET 一个类似于字典的对象,包含 HTTP GET 的所有参数.详情请参考 QueryDict 对象. 2.H ...

  2. 转:Ubuntu下用Sublime输入中文

    最近用上ubuntu跑theano,碰到的一个问题就是用sublime编辑代码的时候无法输入中文. 读代码经常要写注释不能用中文是在是麻烦. 曾经考虑过使用别的文本编辑器,但是sublime的用户界面 ...

  3. redis的二种启动方式

    .直接启动  进入redis根目录,执行命令:  #加上‘&’号使redis以后台程序方式运行 1 ./redis-server & 2.通过指定配置文件启动  可以为redis服务启 ...

  4. NIO编程介绍

    代码: package bhz.nio; import java.io.IOException; import java.net.InetSocketAddress; import java.nio. ...

  5. 有关Zedgraph的功能扩展的笔记

    1.坐标轴范围.刻度调整后需要加上下面的语句才能刷新:     zedGraphControl1.AxisChange();     zedGraphControl1.Refresh(); 2.坐标范 ...

  6. C# XML文件读取

    using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; usin ...

  7. mediawiki的安装

    1. yum install httpd php pcre php-mysql php-pear php-pecl-apc mysql-server ImageMagick sendmail php- ...

  8. Page directive: illegal to have multiple occurrences of contentType with different values

    org.apache.jasper.JasperException: /commons/meta.jsp(1,1) PWC5988: Page directive: illegal to have m ...

  9. 如何在windows下安装mongoDB扩展

    安装环境   系统环境:Windows 10 64位   Apache版本:2.4.9   PHP版本:5.5.12   MongoDB版本:3.2.6   Wamp版本:wamp 2.5 86位   ...

  10. java 多线程下载文件 以及URLConnection和HttpURLConnection的区别

    使用 HttpURLConnection 实现多线程下载文件 注意GET大写//http public class MultiThreadDownload { public static void m ...