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
cand 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 ...
随机推荐
- Leetcode93. Restore IP Addresses复原IP地址
给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255.11.135", ...
- DataTime 和 时间转化
如果知道tostring 的字符串格式那么可以根据字符串格式转化成 DateTime string timeText = DateTime.Now.ToString("yy/MM/dd HH ...
- Django缓存机制以及使用redis缓存数据库
目录 Django 配置缓存机制 缓存系统工作原理 Django settings 中 默认cache 缓存配置 利用文件系统来缓存 使用Memcache来缓存: 使用Local-memory来缓存: ...
- 2-sat——输出方案poj3683
一篇讲的详细的博客 https://blog.csdn.net/Hawo11/article/details/74908233 缩点后为什么要建立反图? 如果是按原图处理,选择一个点之后要把所有其后续 ...
- KNN算法和实现
KNN要用到欧氏距离 KNN下面的缺点很容易使分类出错(比如下面黑色的点) 下面是KNN算法的三个例子demo, 第一个例子是根据算法原理实现 import matplotlib.pyplot as ...
- pytorch基础(1)
基本数据类型和tensor import torch import numpy as np #array 和 tensor的转换 array = np.array([,]) tensorArray = ...
- C#可扩展编程之MEF(五):MEF高级进阶
好久没有写博客了,今天抽空继续写MEF系列的文章.有园友提出这种系列的文章要做个目录,看起来方便,所以就抽空做了一个,放到每篇文章的最后. 前面四篇讲了MEF的基础知识,学完了前四篇,MEF中比较 ...
- Windows的进程间通信
对于任何一个现代的操作系统,进程间通信都是其系统结构的一个重要组成部分.而说到Windows的进程(线程)间通信,那就要看是在什么意义上说了.因为正如"Windows的跨进程操作" ...
- java基础温习 -- Thread
启动线程两种方式: 1. 实现Runnable接口: 2. 继承Thread类. 选用:能使用接口,就不用从Thread类继承. 使用继承的方法不够灵活,从这个类继承了就不能从其他类继承: 实现 ...
- CCA Spark and Hadoop 开发者认证技能点【2016只为hadoop达到巅峰】
Required Skills 技能要求: Data Ingest 数据消化: The skills to transfer data between external systems and you ...