题意:给你一个字符串,要你输出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. 解决ie浏览器下载apk或ipa变为zip

    Tomcat/conf/web.xml <mime-mapping> <extension>apk</extension> <mime-type>app ...

  2. verilog task2

    1.问题:串口的发送和接收 系统时钟50Mhz,波特率119200.系统时钟计数约2604个,才是一位数据的传输时间. 模拟接收的任务函数rx_data_task():LSB first task r ...

  3. 彻底弄懂tf.Variable、tf.get_variable、tf.variable_scope以及tf.name_scope异同

    https://blog.csdn.net/qq_22522663/article/details/78729029 1. tf.Variable与tf.get_variabletensorflow提 ...

  4. vue中使用vue-router切换页面时滚动条自动滚动到顶部的方法

    原文:http://www.jb51.net/article/129270.htm main.js入口文件配合vue-router写这个 router.afterEach((to,from,next) ...

  5. asp相关知识整理

    WWW----World Wide Web(万维网) URL----Uniform Resource Locator(统一资源定位符) HTTP----Hyper Text Transfer Prot ...

  6. 面向服务的架构(SOA)演变图片

    公司项目演变 成熟的公司项目结构 对比 总线-服务的注册与发现

  7. Vim完全教程

    一.简介 世界上只有三种编辑器,EMACS.VIM和其它.   我们所处的时代是非常幸运的,有越来越多的编辑器,相对于古老的VIM和EMACS,它们被称为现代编辑器.我们来看看这两个古董有多大年纪了: ...

  8. mysql技术内幕之常规使用

    mysql中:终止语句方法: 1.在语句结尾处,输入分号(:)表示语句到此结束 2.使用\g(意思是go) \G以垂直的方式显示结果,每行显示一个值 数据库:数据库中包含表,对表中数据执行插入,检索, ...

  9. iOS.CocoaPods.0

    1. CocoaPods CocoaPods 是Objective-C (iOS and OS X) projects 的依赖管理器. A CocoaPod (singular) is a speci ...

  10. “Interrupted by header callback: Server reports Content-Length”如何解决

    mock初始化时的错误信息如下: Downloading Packages: [SKIPPED] systemd--.fc25.x86_64.rpm: Already downloaded [SKIP ...