题意:给你一个字符串,要你输出1-len的字串出现的最大次数。

/** @xigua */
#include <stdio.h>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <cstring>
#include <queue>
#include <set>
#include <string>
#include <map>
#include <climits>
#define PI acos(-1)
#define rep(a,b,c) for(int (a)=(b); (a)<(c); ++(a))
#define drep(a,b,c) for(int (a)=(b); (a)>(c); --(a))
#define CLR(x) memset(x, 0, sizeof(x))
#define sf scanf
#define pf printf
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = * + ;
const int ma = 1e5 + ;
const int mod = 1e9 + ;
const int INF = 1e8 + ;
const ll inf = 1e17 + ;
const db eps = 1e-;
const int MAXN = 2e5+1e3;
struct SAM{
int ch[maxn<<][];
int fa[maxn<<], len[maxn<<];
int cnt, last, root;
void init() {
root=;
memset(ch, , sizeof(ch));
memset(fa, , sizeof(fa));
last=cnt=root;
}
void add(int c) {
int p=last, np=last=++cnt;
len[np]=len[p]+;
while(!ch[p][c] && p) {
ch[p][c]=np;
p=fa[p];
}
if (p==) fa[np]=;
else {
int q = ch[p][c];
if(len[p] + == len[q]) {
fa[np] = q;
}
else {
int nq = ++cnt;
len[nq] = len[p] + ;
memcpy(ch[nq], ch[q], sizeof ch[q]);
fa[nq] = fa[q];
fa[q] = fa[np] = nq;
while(ch[p][c] == q && p) {
ch[p][c] = nq;
p = fa[p];
}
}
}
}
int find(char *s) {
int p=root, l=, c=;
int lenn=strlen(s);
for(int i = ; i < lenn; i++) {
if(ch[p][s[i] - 'a']) {
p = ch[p][s[i] - 'a'];
c++;
}
else {
while(p&&!ch[p][s[i]-'a']) p=fa[p];
if (!p) c=, p=;
else c=len[p]+, p=ch[p][s[i]-'a'];
}
l = max(l, c);
}
printf("%d\n", l);
}
}sam;
char s[maxn];
int c[maxn<<], pt[maxn<<], f[maxn];
void innt() {
memset(pt, , sizeof(pt));
memset(c, , sizeof(c));
memset(f, , sizeof(f));
}
void top() {
for (int i=; i<=sam.cnt; i++)
c[sam.len[i]]++;
for (int i=; i<=sam.cnt; i++)
c[i]+=c[i-];
for (int i=sam.cnt; i>=; i--)
pt[c[sam.len[i]]--]=i; // /*拓扑排序*/ //
/*每个子串对应相应的pt*/
}
int dp[maxn];
void solve() {
innt();
scanf("%s", s);
int lenn=strlen(s);
sam.init();
for (int i=; i<lenn; i++) {
sam.add(s[i]-'a');
}
top();
memset(dp, , sizeof(dp));
int p=sam.root;
for (int i=; i<lenn; i++) {
p=sam.ch[p][s[i]-'a'];
if (p) dp[p]++; //先找出所有子串令dp[p]=1
}
for (int i=sam.cnt; i>=; i--) {
p=pt[i];
if (sam.fa[p]) dp[sam.fa[p]]+=dp[p];
/*该子串(a)还应加上作为串(aa)的个数*/
}
for (int i=; i<=sam.cnt; i++) {
f[sam.len[i]]=max(f[sam.len[i]], dp[i]);
}
for(int i=lenn-;i>=;--i){
if (f[i]<f[i+]) f[i]=f[i+];
}
for (int i=; i<=lenn; i++)
printf("%d\n", f[i]);
}
int main() {
int t = , cas = ;
//freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
//scanf("%d", &t);
while(t--) {
// printf("Case %d: ", cas++);
solve();
}
return ;
}

最新文章

  1. 【Unity3d】3d网页游戏场景打包与加载
  2. 基础学习day09--内部类
  3. 转:数据包经由路由转发时源、目的IP地址及MAC地址变化情况
  4. JAVA构造器、this、super
  5. Bootstrap,Foundation和TypeScript
  6. 座IO理解力
  7. python之二维码生成
  8. 基于爬取百合网的数据,用matplotlib生成图表
  9. Js常用的函数
  10. php laravel加密 form表单认证 laravel分页
  11. 新手必备的Linux知识
  12. (网页)jquery-qrcode.js生成二维码
  13. Mail.Ru Cup 2018 Round 3
  14. 《剑指offer》用两个栈实现队列
  15. AGC 007D.Shik and Game(DP)
  16. Go语言栈定义及相关方法实现
  17. 【亲测】关于HTTP协议~
  18. 质因子分解(Pollard_Rho法)
  19. dzzoffice协同办公平台与onlyoffice在线协作平台安装与部署
  20. 【Codeforces】CF 165 E Compatible Numbers(状压dp)

热门文章

  1. PERL 正则表达式简介
  2. C++批量注释代码段取消注释代码段快捷键
  3. 互联网进行限流策略的Semaphore信号量使用
  4. HDU 1255 覆盖的面积(线段树面积并)
  5. [leetcode]692. Top K Frequent Words K个最常见单词
  6. fastjson的常用方法
  7. FragmentManager V4包下 应该用FragmentActivity
  8. WEB框架之Django实现分页功能
  9. C#生成二维码(可保存二维码图片)
  10. ubuntu远程桌面、VNC(转载)