Codeforces B. String

4 篇文章 0 订阅

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|)), that xi < 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).

Examples

input

Copy

aa
2

output

Copy

a

input

Copy

abc
5

output

Copy

bc

input

Copy

abab
7

output

Copy

b

Note

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

题目大意:给出一个字符串,要求你在这个字符串的所有子串中找出第k大的子串。

解题思路:首先找出所有子串再去排序找第k大师不可能的了。数据量是不允许的。所以,可以把字符串中所有长度为1的字符放进set里面,还要记录该字符的下一位置,需要用到multiset和结构体,而且set具有自动排序功能,所以很方便。每次都去除set中的首元素,然后根据这个元素中标记的下一个字符的位置,找出下一个长度的子串,依次模拟,进行k次就可以了。

题目链接:https://codeforces.com/problemset/problem/128/B

 

/*
@Author: Top_Spirit
@Language: C++
*/
#include <bits/stdc++.h>
using namespace std ;
typedef pair < string, int > P;
const int Maxn = 1e5 + 10 ;

string s ;
multiset < P > se ;
int k ;
P ans ;

int main (){
    cin >> s ;
    int len = s.size() ;
    for (int i = 0; i < len; i++){
        P p ;
        p.first = s[i] ;
        p.second = i + 1 ;
        se.insert(p) ;
    }
    cin >> k ;
    int cnt = 0 ;
    while (!se.empty()){
        auto it = se.begin() ;
        P tmp = *it ;
        cnt++ ;
        if (cnt == k) {
            ans = tmp ;
            break ;
        }
        se.erase (it ) ;
        string str ;
        if (tmp.second < len){
            str = tmp.first ;
            str += s[tmp.second] ;
            P q ;
            q.first = str ;
            q.second = tmp.second + 1 ;
            se.insert(q) ;
        }
    }
    if (cnt == k) cout << ans.first << endl ;
    else cout << "No such line." << endl ;
    return 0 ;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值