Hackerrank--Ashton and String(后缀数组)
Ashton appeared for a job interview and is asked the following question. Arrange all the distinct substrings of a given string in lexicographical order and concatenate them. Print the Kth character of the concatenated string. It is assured that given value of K will be valid i.e. there will be a Kth character. Can you help Ashton out with this?
Input Format
First line will contain a number T i.e. number of test cases.
First line of each test case will contain a string containing characters (a−z) and second line will contain a number K.Output Format
Print Kth character ( the string is 1 indexed )Constraints
1≤T≤5
1≤length≤105
K will be an appropriate integer.Sample Input #00
1
dbac
3
Sample Output #00
c
Explanation #00
The substrings when arranged in lexicographic order are as follows
a, ac, b, ba, bac, c, d, db, dba, dbac
On concatenating them, we get
aacbbabaccddbdbadbac
The third character in this string is
c
and hence the answer.
题意:给出一个字符串,把这个字符串的所有字串按字典序升序拼接起来得到一个大的字符串。求该字符串的第k个字符。
思路:该字符串的字串按字典序排序后的结果其实就是后缀数组中根据lcp一个个数出来的字串。。。
Accepted Code:
#include <string>
#include <iostream>
#include <algorithm>
using namespace std; const int MAX_N = ;
typedef long long LL;
int n, k, sa[MAX_N], rk[MAX_N], lcp[MAX_N], tmp[MAX_N]; 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; void solve(LL k) {
n = S.length();
construct_sa(S, sa);
construct_lcp(S, sa, lcp); for (int i = ; i < n; i++) {
int L = lcp[i];
int left = n - sa[i + ];
LL sum = (L + + left) * (LL)(left - L) / ;
if (k > sum) {k -= sum;}
else {
for (int j = L + ; j <= left; j++) {
if (k <= j) {
int index = sa[i + ] + k;
cout << S[index - ] << endl;
return ;
} else {
k -= j;
}
}
}
}
}
int main(void) {
ios::sync_with_stdio(false);
int T;
cin >> T;
while (T--) {
LL k;
cin >> S >> k;
solve(k);
}
return ;
}
Hackerrank--Ashton and String(后缀数组)的更多相关文章
- 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 ~ ...
- POJ3729 Facer’s string 后缀数组
Fa ...
- hdu 5030 Rabbit's String(后缀数组&二分法)
Rabbit's String Time Limit: 40000/20000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- hdu-6194 string string string 后缀数组 出现恰好K次的串的数量
最少出现K次我们可以用Height数组的lcp来得出,而恰好出现K次,我们只要除去最少出现K+1次的lcp即可. #include <cstdio> #include <cstrin ...
- [Codechef CHSTR] Chef and String - 后缀数组
[Codechef CHSTR] Chef and String Description 每次询问 \(S\) 的子串中,选出 \(k\) 个相同子串的方案有多少种. Solution 本题要求不是很 ...
- 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 ...
随机推荐
- h5对接jssdk支付分并调用开启支付分页面
1.ws.config签名 调用ticket等获取ws.config的签名,下面会调用方法再调用方法时需要再次按照调用方法的签名 wx.config({ debug: true, // 开启调试模 ...
- 网站时间显示——基于Date
网站时间显示 代码实现如下: =============css样式=================== <style> #show{ width: 460px; height: 100p ...
- 多项式模板&题目整理
注:多项式的题目,数组应开:N的最近2的整数次幂的4倍. 多项式乘法 FFT模板 时间复杂度\(O(n\log n)\). 模板: void FFT(Z *a,int x,int K){ static ...
- bzoj2209 括号序列
题意:给你一个括号序列.操作1:询问需要更改多少个括号使之匹配. 操作2:反转序列,左括号变成右括号. 操作3:翻转序列,倒置. 标程: #include<cstdio> #include ...
- python 简单的图片比较
# by movie on 2019/12/18 from PIL import Image from PIL import ImageChops path1 = 'images/trumpA689. ...
- crontab[计划任务],tar[压缩],grep[查找]
计划任务:1.新建一个计划任务:crontab -e -----> 3*/1 * * * * date >> /tmp/data.txt查看计划任务:crontab -l.如果超过6 ...
- Java写爬虫代码时报org.apache.http.client.ClientProtocolException: URI does not specify a valid host异常的处理
异常原因是url写错,导致无法解析 比如:这个报错就是因为写了两个“http:”导致该无法解析
- kafka集群搭建文档
kafka集群搭建文档 一. 下载解压 从官网下载Kafka,下载地址http://kafka.apache.org/downloads.html 注意这里最好下载scala2.10版本的kafka, ...
- Java学习之一(引用相关)
1.Java概述 首先,Java是一门面向对象的编程语言.相对于C/C++等语言,Java中没有指针,但是这不代表指针等知识不重要:Java中不存在多继承但是存在多接口.在我自己的学习过程之中,我偏向 ...
- HTML编码的用户输入------阻止向Controller的方法传入参数时用链接注入javascript代码或者HTML标记