codeforces 128 B. String

链接:http://codeforces.com/contest/128/problem/B

B. String
time limit per test
 2 seconds
memory limit per test
 256 megabytes
input
 standard input
output
 standard output

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 < ixj = 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).

Sample test(s)
input
aa
2
output
a
input
abc
5
output
bc
input
abab
7
output
b
Note

In the second sample before string "bc" follow strings "a", "ab", "abc", "b".



题意:字典序排序子串,找出子串中的第n大。

做法:因为最多让你找第100000大,所以可以用优先队列。先放入单个的。然后不断取出最小的,然后添加一个 其后面的字母,然后再放回队列。 队列里len个串,最多放n次,所以复杂度 log(len)*n。 


啊。。。我觉得我的代码效率很慢。。然后还是用cin string 的情况下。。别人的题解就T了。。好奇怪。。
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
struct Node {
    string str;
    int pos;
    friend bool operator < (Node const& n1, Node const& n2) {
        return n2.str < n1.str;
    }
};
priority_queue<Node> que;
int main() {
    //freopen("in.txt", "r", stdin);
    int k;
    string str;
    cin >> str;
    cin >> k;
    que.push({string{'z' + 1}, INF});
    for (int i = 0; i < str.size(); ++i) {
        que.push({string{str[i]}, i});
    }
    Node i;
    while (k-- && !que.empty()) {
        i = que.top();
        que.pop();
        if (i.pos == str.size() - 1 || i.pos == INF)
            continue;
        que.push({i.str + str[i.pos + 1], i.pos + 1});
    }
    if (k && que.empty())
        cout << "No such line." << endl;
    else
        cout << i.str << endl;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值