您的位置:首页 > 其它

codeforces 128B - String

2016-09-06 22:10 369 查看
One day in the IT lesson Anna and Maria learned about the lexicographic order.

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|)),
thatxi < 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).

AC代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <queue>
using namespace std;
char c[100001];
int cn;
string s[100001];
int ed[100001];
struct Cmp{
bool operator()(int a, int b){
return s[a] > s[b];
}
};
typedef priority_queue<int, vector<int>, Cmp> pq;
pq pri;
int main() {
while ( scanf("%s", c) != EOF ){
cn = strlen(c);
pq().swap(pri);//将队列清空
for (int ci = 0; ci < cn; ci++){
s[ci] = c[ci];
ed[ci] = ci;
pri.push(ci);
}
int k;
scanf("%d", &k);
for (int ki = 0; ki < k; ki++) {
if ( pri.empty() ) {
puts("No such line.");
break;
}
int t = pri.top();
pri.pop();
if ( ki+1 == k ) puts(s[t].c_str() );
ed[t]++;
if ( ed[t] != cn ){
s[t].push_back(c[ed[t]]);
pri.push(t);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: