SPOJ - NSUBSTR(长度为1-len的字串出现的最大次数
题意:给你一个字符串,要你输出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的字串出现的最大次数的更多相关文章
- SPOJ - Distinct Substrings,求不同的字串个数!
DISUBSTR - Distinct Substrings 题意:给你一个长度最多1000的字符串,求不相同的字串的个数. 思路:一个长度为n的字符串最多有(n+1)*n/2个,而height数组已 ...
- (字符串)最长公共字串(Longest-Common-SubString,LCS)
题目: 给定两个字符串X,Y,求二者最长的公共子串,例如X=[aaaba],Y=[abaa].二者的最长公共子串为[aba],长度为3. 子序列是不要求连续的,字串必须是连续的. 思路与代码: 1.简 ...
- java 蓝桥杯算法提高 字串统计
思路:这道题用HashMap来保存枚举的字串,key值保存字串-value值保存字串所出现的次数: 通过for循环并使用subString()方法枚举所有符合要求的子串maxStr记录 ...
- 【spoj NSUBSTR】 Substrings
http://www.spoj.com/problems/NSUBSTR/ (题目链接) 题意 给出一个字符串S,令${F(x)}$表示S的所有长度为x的子串出现次数的最大值.求${F(1)..... ...
- SPOJ NSUBSTR (后缀自动机)
SPOJ NSUBSTR Problem : 给一个长度为n的字符串,要求分别输出长度为1~n的子串的最多出现次数. Solution :首先对字符串建立后缀自动机,在根据fail指针建立出后缀树,对 ...
- POJ - 2406 ~SPOJ - REPEATS~POJ - 3693 后缀数组求解重复字串问题
POJ - 2406 题意: 给出一个字符串,要把它写成(x)n的形式,问n的最大值. 这题是求整个串的重复次数,不是重复最多次数的字串 这题很容易想到用KMP求最小循环节就没了,但是后缀数组也能写 ...
- SPOJ NSUBSTR Substrings
题意 dt { font-weight: bold; margin-top: 20px; padding-left: 35px; } dd { box-shadow: 3px 3px 6px #888 ...
- SPOJ - NSUBSTR 后缀自动机板子
SPOJ - NSUBSTR #include<bits/stdc++.h> #define LL long long #define fi first #define se second ...
- C#/WPF 计算字串的真实长度,调整控件的宽度
下面函数是经常用到的计算字串长度的方法: private double MeasureTextWidth(String str, string fontName, double fon ...
随机推荐
- log4j最中意的配置
log4j.rootLogger=DEBUG, Console, logfile log4j.appender.Console=org.apache.log4j.ConsoleAppender log ...
- Django项目的创建与管理和pycharm与Github的秘密
随笔 - 174 文章 - 21 评论 - 19 Django项目创建与管理 1.主题 这部分教程主要介绍如何通过Pycharm创建.管理.运行一个Django工程.对于Django模块的相关 ...
- 204. Count Primes (Integer)
Count the number of prime numbers less than a non-negative number, n. 思路:寻找质数的方法 class Solution { pu ...
- js改变表单的内容样式
一.改变单个样式 var obj = document.getElementById("id"); obj.style.cssText = " display: ...
- Codeforces Beta Round #55 (Div. 2)
Codeforces Beta Round #55 (Div. 2) http://codeforces.com/contest/59 A #include<bits/stdc++.h> ...
- Adb logcat 抓日志
http://blog.csdn.net/hujiachun1234/article/details/43271149 http://www.cnblogs.com/medsonk/p/6344373 ...
- php cli命令 自定义参数传递
所有的PHP发行版,不论是编译自源代码的版本还是预创建的版本,都在默认情况下带有一个PHP可执行文件.这个可执行文件可以被用来运行命令行的PHP程序.要在你的系统上找到这个可执行文件,就要遵照下面的步 ...
- mysql 索引 create_time 加explain关键字是否走索引
SELECT * FROM t_user WHERE email='217@xxg.com'; --1.725 --加email索引之后 0.003 SELECT * FROM t_user WHE ...
- 865. Smallest Subtree with all the Deepest Nodes 有最深节点的最小子树
[抄题]: Given a binary tree rooted at root, the depth of each node is the shortest distance to the roo ...
- VS unable to update auto-refresh path。。。。
手工创建提示报错的路径,重新生成,成功