I want to find lexicographically Kth smallest substring of a given string when the duplicate substrings are allowed.
Suppose we are given a string abc then its substrings in lexicographical order are {a,ab,abc,b,c}, now suppose we are given K = 3 then ans is abc.
Now suppose we are given string aaa then all its substrings are {a,a,a,aa,aaa,aa} so now if K = 4 then we output aa.
However I came across the following code on codeforces but i am not able to understand it. any help is greatly appreciated.
char s [MaxN];
bool b [MaxN];
int k, n;
void solve (vector <int> v)
{
int i, j;
int64 p, q;
char c;
vector <int> w;
for (c = 'a'; c <= 'z'; c++)
{
p = q = 0;
w.clear ();
for (j = 0; j < (int) v.size (); j++)
{
i = v[j];
if (s[i] == c)
{
w.push_back (i + 1);
p++;
q += n - i;
}
}
if (k < q)
break;
k -= q;
}
assert (c <= 'z');
putchar (c);
if (k < p)
return;
k -= p;
solve (w);
}
int main (void)
{
int i;
while (scanf (" %s %d", s, &k) != EOF)
{
n = (int) strlen (s);
if (k > ((((int64) n) * (int64) (n + 1)) >> 1LL))
{
printf ("No such line.\n");
continue;
}
k--;
vector <int> v;
for (i = 0; i < n; i++)
v.push_back (i);
solve (v);
putchar ('\n');
}
return 0;
}
Here is the link to question http://codeforces.com/problemset/problem/128/B