E. String
String x is lexicographically less than string y, if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that xi < yi, and for any j (1 ≤ j < i) xj = yj. Here |a| denotes the length of the string a. The lexicographic comparison of strings is implemented by operator < in modern programming languages.
The teacher gave Anna and Maria homework. She gave them a string of length n. They should write out all substrings of the given string, including the whole initial string, and the equal substrings (for example, one should write out the following substrings from the string “aab”: “a”, “a”, “aa”, “ab”, “aab”, “b”). The resulting strings should be sorted in the lexicographical order. The cunning teacher doesn’t want to check all these strings. That’s why she said to find only the k-th string from the list. Help Anna and Maria do the homework.
Input
The first line contains a non-empty string that only consists of small Latin letters (“a”-“z”), whose length does not exceed 105. The second line contains the only integer k (1 ≤ k ≤ 105).
Output
Print the string Anna and Maria need — the k-th (in the lexicographical order) substring of the given string. If the total number of substrings is less than k, print a string saying “No such line.” (without the quotes).
Examples
inputCopy
aa
2
outputCopy
a
inputCopy
abc
5
outputCopy
bc
inputCopy
abab
7
outputCopy
b
Note
In the second sample before string “bc” follow strings “a”, “ab”, “abc”, “b”.
需知:
1:长度为n的字符串最多有n*(n+1)/2个连续子串.
2:并且int容纳大概10的3次方而题目要求10的5次方,所以代码中用long long int (最大大概10的九次方)
代码理解:(abab)
先插入 a,b,a,d;同时记录每个字符相连的下一个字符;
排序后a,a,b,b;
然后a的下一位为b;
所以ab插入,a删除
插入后的排序为a,ab,b,b;
再插入这个a与相连下一位b的结合–ab;
排序后为ab,ab,b,b;
同理往下整。
如果满足要求的第几个停止。并且子串长度成长为它的最长子串时,开始排序set在他之后的字符的子串
#include<iostream>
#include<set>
#include<string>
#include<cstdio>
#include<string.h>
#include<cstring>
#include<algorithm>
using namespace std;
typedef pair<string,int> p;
set<p>sett;
string ss;
int k;
string a[26]={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
int main()
{
while(cin>>ss>>k)
{
long long int n=ss.size();
if(k>((n*(n+1))/2))
{
cout<<"No such line."<<endl;
continue;
}
sett.clear();
for(int i=0;i<n;i++)
{
sett.insert(p(a[ss[i]-'a'],i+1));
}
k--;
while(k--)
{
p now=*sett.begin();
sett.erase(sett.begin());
if(now.second==n)
continue;
now.first+=ss[now.second];
now.second++;
sett.insert(now);
}
cout<<sett.begin()->first<<endl;
}
}