P14584 [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 $k^{th}$ character of the concatenated string. It is assured that given value of $\boldsymbol{\mbox{k}}$ will be valid i.e. there will be a $k^{th}$ character. Can you help Ashton out with this?

For example, given the string $s=\mathtt{abc}$, its distinct substrings are $[a,\:ab,\:abc,\:abcd,\:b,\:bc,\:bcd,\:c,\:cd,\:d]$. Sorted and concatenated, they make the string $aababcabcdbbcbcdccdd$. If $K=5$ then, the answer is $\boldsymbol{b}$, the $5^{th}$ character of the 1-indexed concatenated string.

Note We have distinct substrings here, i.e. if string is aa, it's distinct substrings are a and aa.

Function Description

Complete the ashtonString function in the editor below. It should return the $k^{th}$ character from the concatenated string, 1-based indexing.

ashtonString has the following parameters:

  • s: a string

  • k: an integer

Input Format

The first line will contain an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.

Each of the subsequent $\boldsymbol{\boldsymbol{t}}$ pairs of lines is as follows:

  • The first line of each test case contains a string, $\boldsymbol{\mathrm{~S~}}$.

  • The second line contains an integer, $\boldsymbol{\mbox{k}}$.

Constraints

$1\leq t\leq5$

$1\leq|s|\leq10^5$

Each character of string $s\in a s c i i[a-z]$

$\boldsymbol{\mbox{k}}$ will be an appropriate integer.

Output Format

Print the $k^{th}$ character (1-based index) of the concatenation of the ordered distinct substrings of $\boldsymbol{\mathrm{~S~}}$.

Sample Input
1
dbac
3

Sample Output
c

Explanation

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.

输入格式

见题面

输出格式

见题面

样例输入
1 dbac 3
样例输出
c