#P3975. K-th Smallest Substring

    ID: 17223 Type: Default 1000ms 256MiB

K-th Smallest Substring

K-th Smallest Substring

In order to boost her IQ, ZJY started studying string theory. One day, she encountered the following problem in the book String Theory: given a string of length \( n \), find its \( k \)-th smallest substring.

More formally, consider all non-empty contiguous substrings of a given string \( s \). Sort these substrings in lexicographical order (duplicates are counted separately). You are required to output the substring that appears in the \( k \)-th position in this sorted order. It is guaranteed that \( 1 \le k \le \frac{n(n+1)}{2} \).

inputFormat

The input consists of two lines:

  1. The first line contains a non-empty string \( s \) of length \( n \).
  2. The second line contains a positive integer \( k \).

outputFormat

Output the \( k \)-th smallest substring after sorting all non-empty contiguous substrings of \( s \) in lexicographical order.

sample

abc
3
abc