题目链接

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(后缀数组)的更多相关文章

  1. hdu 3553 Just a String (后缀数组)

    hdu 3553 Just a String (后缀数组) 题意:很简单,问一个字符串的第k大的子串是谁. 解题思路:后缀数组.先预处理一遍,把能算的都算出来.将后缀按sa排序,假如我们知道答案在那个 ...

  2. hdu 6194 沈阳网络赛--string string string(后缀数组)

    题目链接 Problem Description Uncle Mao is a wonderful ACMER. One day he met an easy problem, but Uncle M ...

  3. HDU 6194 string string string (后缀数组)

    题意:给定一个字符串,问你它有多少个子串恰好出现 k 次. 析:后缀数组,先把height 数组处理出来,然后每次取 k 个进行分析,假设取的是 i ~ i+k-1,那么就有重复的,一个是 i-1 ~ ...

  4. POJ3729 Facer’s string 后缀数组

                                                                                                      Fa ...

  5. hdu 5030 Rabbit&#39;s String(后缀数组&amp;二分法)

    Rabbit's String Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  6. hdu-6194 string string string 后缀数组 出现恰好K次的串的数量

    最少出现K次我们可以用Height数组的lcp来得出,而恰好出现K次,我们只要除去最少出现K+1次的lcp即可. #include <cstdio> #include <cstrin ...

  7. [Codechef CHSTR] Chef and String - 后缀数组

    [Codechef CHSTR] Chef and String Description 每次询问 \(S\) 的子串中,选出 \(k\) 个相同子串的方案有多少种. Solution 本题要求不是很 ...

  8. HDU5008 Boring String Problem(后缀数组 + 二分 + 线段树)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5008 Description In this problem, you are given ...

  9. HDU5853 Jong Hyok and String(二分 + 后缀数组)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5853 Description Jong Hyok loves strings. One da ...

随机推荐

  1. NOIP2018游记 & 退役记

    NOIP2018游记 & 退役记 我是一名来自湖北武汉华中师大一附中的高二\(OIer\),学习\(OI\)一年,今年去参加\(NOIP\),然后退役.这是一篇\(NOIP2018\)的游记, ...

  2. laydate实现设置最小时间为当前时间

    当前时间之前的日期不能选择,添加个min,主要代码如下: var now = new Date(); startTime= laydate.render({ elem: '#test1', type: ...

  3. 二分+2-sat——hdu3062

    hdu3622升级版 注意要保留两位小数 /* 给定n对圆心(x,y),要求从每对里找到一个点画圆,不可相交 使得最小半径最大 二分答案,设最小半径为r 然后两两配对一次进行判断,在2-sat上连边即 ...

  4. C#一般处理程序设置和读取session(session报错“未将对象引用设置到对象的实例”解决)

    登陆模块时,用到了session和cookie.在一般处理程序中处理session,一直报错.最后找到问题原因是需要调用 irequiressessionstate接口. 在ashx文件中,设置ses ...

  5. JAVA 类加载机制学习笔记

    JAVA 类生命周期 如上图所示,Java类的生命周期如图所示,分别为加载.验证.准备.解析.初始化.使用.卸载.其中验证.准备.解析这三个步骤统称为链接. 加载:JVM根据全限定名来获取一段二进制字 ...

  6. springmvc jar包下载

    (1) springmvc jar包下载 提供地址 - qq_31307253的博客 - CSDN博客https://blog.csdn.net/qq_31307253/article/details ...

  7. Excel 2016在大数据分析领域有了很多的改善

    Excel 2016在大数据分析领域有了很多的改善 通常,我们会把大数据分析的整个过程分为五个阶段: 获取获取,数据分析,可视化,发布报告,应用报告. 在获取数据方面,Excel 2016相对Exce ...

  8. LOJ10157——皇宫看守(树形DP)

    传送门:QAQQAQ 题意:在一个树上放置守卫,使每一个节点都至少有相邻一节点放置守卫,使最终经费最少 思路:树形DP 首先会想到没有上司的舞会,0表示不放守卫,1表示放守卫,但考虑到对于当前点不放守 ...

  9. PAT甲级——A1001A+BFormat

    Calculate a+b and output the sum in standard format -- that is, the digits must be separated into gr ...

  10. Synchronized理解及用法

    加锁: 1.同步实例方法,锁是当前实例对象 2.同步类方法,锁的是当前类对象 3.同步代码块,锁是括号里面的对象 原理: JVM内置锁通过synchronized使用,通过内部对象Monitor(监视 ...