后缀自动机+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();
}

最新文章

  1. 0.AutoMapper核心
  2. Shell编程中括号判断中赋值语句和判断语句
  3. Kafka原理与java simple producer示例
  4. java关闭流,解压缩后的清除
  5. python中split函数的使用
  6. GUI为什么不设计为多线程(用户事件和底层事件的流程是相反的,每层都加锁效率太低,共用一把锁那就是单线程)
  7. xcode initWithCoder\awakeFromNib\layoutSubviews
  8. 基于MAC OS 操作系统安装、配置mysql
  9. iOS开发一些小技巧
  10. js校验身份证
  11. 【Tomcat】batch获得war包
  12. 企业级nosql数据库应用与实战-redis
  13. qsort.c源码
  14. .net core使用Pipelines进行消息IO合并
  15. Nginx 日志处理
  16. 【6集iCore3_ADP触摸屏驱动讲解视频】6-4 底层驱动之SDRAM读写(上)
  17. 强化学习--DDPG---tensorflow实现
  18. Alpha阶段敏捷冲刺(四)
  19. Angular 动态组件
  20. MyEclipse安装EGit插件方法

热门文章

  1. BFC以及margin的深入探究
  2. PHP中composer的安装和使用
  3. PHP编译安装系列
  4. 爬虫中urllib库
  5. 记一则css3计算
  6. GNU/Linux操作系统总览
  7. 微信小程序从入坑到放弃之坑十二:navigator无法跳转的坑
  8. Spring oxm入门实例
  9. python算法之归并排序
  10. Mysql 源码编译安装 ( 5.5 、5.6 共存 )