Hackerrank--String Function Calculation(后缀数组)
Jane loves string more than anything. She made a function related to the string some days ago and forgot about it. She is now confused about calculating the value of this function. She has a string T with her, and value of string S over function f can be calculated as given below:
f(S)=|S|∗NumberoftimesSoccuringinTJane wants to know the maximum value of f(S) among all the substrings (S) of string T. Can you help her?
Input Format
A single line containing string T in small letter('a' - 'z').Output Format
An integer containing the value of output.Constraints
1 ≤|T|≤ 105Sample Input #00
aaaaaa
Sample Output #00
12
Explanation #00
f('a') = 6
f('aa') = 10
f('aaa') = 12
f('aaaa') = 12
f('aaaaa') = 10
f('aaaaaa') = 6
Sample Input #01
abcabcddd
Sample Output #01
9
Explanation #01
f("a") = 2
f("b") = 2
f("c") = 2
f("ab") = 4
f("bc") = 4
f("ddd") = 3
f("abc") = 6
f("abcabcddd") = 9
Among the function values 9 is the maximum one.
题意:求字符串所有子串中,f(s)的最大值。这里s是某个子串,f(s) = s的长度与s在原字符串中出现次数的乘积。
求得lcp, 转化为求以lcp为高的最大子矩形。
Accepted Code:
#include <string>
#include <iostream>
#include <algorithm>
using namespace std; const int MAX_N = ;
int sa[MAX_N], rk[MAX_N], lcp[MAX_N], tmp[MAX_N], n, k; bool compare_sa(int i, int j) {
if (rk[i] != rk[j]) return rk[i] < rk[j];
int ri = i + k <= n ? rk[i + k] : -;
int rj = j + k <= n ? rk[j + k] : -;
return ri < rj;
} void construct_sa(const string &S, int *sa) {
n = S.length();
for (int i = ; i <= n; i++) {
sa[i] = i;
rk[i] = i < n ? S[i] : -;
} for (k = ; k <= n; k *= ) {
sort(sa, sa + n + , compare_sa); tmp[sa[]] = ;
for (int i = ; i <= n; i++) {
tmp[sa[i]] = tmp[sa[i - ]] + (compare_sa(sa[i - ], sa[i]) ? : );
}
for (int i = ; i <= n; i++) rk[i] = tmp[i];
}
} void construct_lcp(const string &S, int *sa, int *lcp) {
n = S.length();
for (int i = ; i <= n; i++) rk[sa[i]] = i; int h = ;
lcp[] = ;
for (int i = ; i < n; i++) {
int j = sa[rk[i] - ]; if (h > ) h--;
for (; i + h < n && j + h < n; h++) if (S[i + h] != S[j + h]) break; lcp[rk[i] - ] = h;
}
} string S;
int lft[MAX_N], rgt[MAX_N], st[MAX_N], top;
void solve() {
construct_sa(S, sa);
construct_lcp(S, sa, lcp); lcp[n] = n - sa[n];
// for (int i = 1; i <= n; i++) cerr << lcp[i] << ' ';
// cerr << endl;
top = ;
for (int i = ; i <= n; i++) {
while (top && lcp[st[top-]] >= lcp[i]) top--;
if (top) lft[i] = st[top - ] + ;
else lft[i] = ;
st[top++] = i;
}
top = ;
for (int i = n; i > ; i--) {
while (top && lcp[st[top-]] >= lcp[i]) top--;
// attention: rgt[i] should be asigned to st[top - 1]
// rather than st[top - 1] - 1 because lcp[i] is the
// length of the longest common prefix of sa[i] and sa[i + 1].
if (top) rgt[i] = st[top - ];
else rgt[i] = n;
st[top++] = i;
}
long long ans = n;
for (int i = ; i <= n; i++) ans = max(ans, (long long)lcp[i] * (rgt[i] - lft[i] + ));
cout << ans << endl;
} int main(void) {
//ios::sync_with_std(false);
while (cin >> S) solve();
return ;
}
Hackerrank--String Function Calculation(后缀数组)的更多相关文章
- HDU5008 Boring String Problem(后缀数组 + 二分 + 线段树)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5008 Description In this problem, you are given ...
- HDU5853 Jong Hyok and String(二分 + 后缀数组)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5853 Description Jong Hyok loves strings. One da ...
- CF1063F. String Journey(后缀数组+线段树)
题目链接 https://codeforces.com/contest/1063/problem/F 题解 虽然本题有时间复杂度较高但非常好写的做法...... 首先,若答案为 \(k\),则一定存在 ...
- Codeforces 1063F - String Journey(后缀数组+线段树+dp)
Codeforces 题面传送门 & 洛谷题面传送门 神仙题,做了我整整 2.5h,写篇题解纪念下逝去的中午 后排膜拜 1 年前就独立切掉此题的 ymx,我在 2021 年的第 5270 个小 ...
- acdream1116 Gao the string!(hash二分 or 后缀数组)
问题套了一个斐波那契数,归根结底就是要求对于所有后缀s[i...n-1],所有前缀在其中出现的总次数.我一开始做的时候想了好久,后来看了别人的解法才恍然大悟.对于一个后缀来说 s[i...n-1]来说 ...
- 【HDU 5030】Rabbit's String (二分+后缀数组)
Rabbit's String Problem Description Long long ago, there lived a lot of rabbits in the forest. One d ...
- hdu 3553 Just a String (后缀数组)
hdu 3553 Just a String (后缀数组) 题意:很简单,问一个字符串的第k大的子串是谁. 解题思路:后缀数组.先预处理一遍,把能算的都算出来.将后缀按sa排序,假如我们知道答案在那个 ...
- hdu 6194 沈阳网络赛--string string string(后缀数组)
题目链接 Problem Description Uncle Mao is a wonderful ACMER. One day he met an easy problem, but Uncle M ...
- HDU 6194 string string string (后缀数组)
题意:给定一个字符串,问你它有多少个子串恰好出现 k 次. 析:后缀数组,先把height 数组处理出来,然后每次取 k 个进行分析,假设取的是 i ~ i+k-1,那么就有重复的,一个是 i-1 ~ ...
随机推荐
- 集合遍历remove时ConcurrentModificationException异常
1.集合遍历时候,有时候需要remove或add操作,这时候遍历方式可能会影响程序运行 例如: @Test public void test1() { List<Integer> intL ...
- c语言学习笔记 - 二进制文件
在进行文件操作的时候,有时候是用文本的形式存在文件里面,例如用 fprintf(fp,"%d",123) 存一个数据123,实际的存储是已1,2,3这3个ASCII码存入,打开文件 ...
- light oj 1231 dp 多重背包
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> ...
- System.Web.Mvc.FileStreamResult.cs
ylbtech-System.Web.Mvc.FileStreamResult.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, P ...
- Android之TableLayout表格布局
1.相关属性 1.1.常用属性 android:collapseColumns 设置需要被隐藏的列的序列号 android:shrinkColumns 设置允许被收缩的列的序列号 android:st ...
- Ubuntu时间管理方法
1. date 命令主要用于显示以及修改系统时间 2. hwclock 命令用于查看设置硬件时间,以及同步硬件时间与系统时间 # 显示硬件时间hwclock # 设置硬件时间hwclock -set ...
- object and namespace
http://effbot.org/zone/python-objects.htm 几点总结: (1) 类的基本属性 . id, returned by id(obj) . type, returne ...
- 威胁快报|Solr dataimport成挖矿团伙新型利用方式
概述 近日,阿里云安全团队监测到挖矿团伙利用solr dataimport RCE(CVE-2019-0193)作为新的攻击方式对云上主机进行攻击,攻击成功后下载门罗币挖矿程序进行牟利.该团伙使用的恶 ...
- 伸缩布局flex
一.伸缩布局的起源 1.之前我们想要适应不同的浏览器,一般采用的是设置宽度.高度为父级元素的百分比,但是有时候百分比的计算是相当复杂的,加上有时候还有规定的宽度要设置,所以,伸缩布局的出现是我们所急需 ...
- Python3.5 安装 & hello world
1.下载安装python https://www.python.org/downloads/release/python-364/ 2.安装成功运行 python shell 3.或者cmd => ...