题意:给你一个字符串,要你输出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 ;
}

SPOJ - NSUBSTR(长度为1-len的字串出现的最大次数的更多相关文章

  1. SPOJ - Distinct Substrings,求不同的字串个数!

    DISUBSTR - Distinct Substrings 题意:给你一个长度最多1000的字符串,求不相同的字串的个数. 思路:一个长度为n的字符串最多有(n+1)*n/2个,而height数组已 ...

  2. (字符串)最长公共字串(Longest-Common-SubString,LCS)

    题目: 给定两个字符串X,Y,求二者最长的公共子串,例如X=[aaaba],Y=[abaa].二者的最长公共子串为[aba],长度为3. 子序列是不要求连续的,字串必须是连续的. 思路与代码: 1.简 ...

  3. java 蓝桥杯算法提高 字串统计

    思路:这道题用HashMap来保存枚举的字串,key值保存字串-value值保存字串所出现的次数:         通过for循环并使用subString()方法枚举所有符合要求的子串maxStr记录 ...

  4. 【spoj NSUBSTR】 Substrings

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

  5. SPOJ NSUBSTR (后缀自动机)

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

  6. POJ - 2406 ~SPOJ - REPEATS~POJ - 3693 后缀数组求解重复字串问题

    POJ - 2406 题意: 给出一个字符串,要把它写成(x)n的形式,问n的最大值. 这题是求整个串的重复次数,不是重复最多次数的字串 这题很容易想到用KMP求最小循环节就没了,但是后缀数组也能写 ...

  7. SPOJ NSUBSTR Substrings

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

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

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

  9. C#/WPF 计算字串的真实长度,调整控件的宽度

    下面函数是经常用到的计算字串长度的方法:         private double MeasureTextWidth(String str, string fontName, double fon ...

随机推荐

  1. VHS介绍

    Java.JS HTTP流传输(VHS)介绍“我怎么才能让我的视频和Video.js一起玩?”“ 这是我们在使用Vio.js时最常见的问题之一.这是个好问题. 如果有人检查了Vo.js的拷贝,他们的内 ...

  2. python中matplotlib 的简单使用

    1.简单折线图的画图,轴标签.图的颜色,风格,等等参数,本文只介绍最常用的几个参数: import matplotlib.pyplot as plt import numpy as np x = np ...

  3. sqoop2 缺少 jackson-core-asl-1.8.8.jar 和 jackson-mapper-asl-1.8.8.jar 这两个jar包

    [root@spark2 client]# cat /var/log/sqoop2/localhost.2017-12-22.log 十二月 22, 2017 10:29:17 上午 org.apac ...

  4. Python compile() 函数

    Python compile() 函数  Python 内置函数 描述 compile() 函数将一个字符串编译为字节代码. 语法 以下是 compile() 方法的语法: compile(sourc ...

  5. LightOJ - 1030 期望+dp

    题目链接:https://vjudge.net/problem/25907/origin 一个山洞,里面有有1到n个位置,每个位置都有一定的金币,你有一个六面的骰子,一开始你在1,每次摇到了哪个数就往 ...

  6. jsp页面之初体验

    最近在学jsp 在web.xml页面中学到了如何让一个页面第一个启动

  7. vue 打包

    1.assetsPublicPath路径改为‘./’(两处) 2.cmd运行  cnpm run build 3.生成的dist文件即可本地访问

  8. TZOJ 2546 Electricity(去掉割点后形成的最大连通图数)

    描述 Blackouts and Dark Nights (also known as ACM++) is a company that provides electricity. The compa ...

  9. TZOJ 3709:Number Maze(广搜记录前驱)

    描述 You are playing one game called "Number Maze". The map of an example is shown in the fo ...

  10. mysql事务四大特性

    本篇讲诉数据库中事务的四大特性(ACID)和简单操作 ⑴ 原子性(Atomicity) 原子性是指事务包含的所有操作要么全部成功,要么全部失败回滚,这和前面两篇博客介绍事务的功能是一样的概念,因此事务 ...