Codeforces 128B. String(优先队列,模仿dijkstra求最短路)

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

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".


居然看了题解才做出来..

这个方法很像dijkstra算法里往优先队列塞元素的办法,可以保证,每次取出来的都是按顺序递增最小的,第k次取出来的就是第k个

一开始,要把长度为1的子串全部打入优先队列

接下来,依次取出优先队列里头元素,表示字典序当前最小的子串,很显然,第一个取出来就是所有子串最小的

取出来后,如果可以往后延长字符串,就把新的子串放入优先队列里。

可以证明这样的做法一定是最好的,我不想证明了,具体可以看看dijkstra求最短路里的证明,我想方法是类似的。


//Hello. I'm Peter.
#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<cctype>
#include<ctime>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double ld;
#define peter cout<<"i am peter"<<endl
#define input freopen("data.txt","r",stdin)
#define randin srand((unsigned int)time(NULL))
#define INT (0x3f3f3f3f)*2
#define LL (0x3f3f3f3f3f3f3f3f)*2
#define gsize(a) (int)a.size()
#define len(a) (int)strlen(a)
#define slen(s) (int)s.length()
#define pb(a) push_back(a)
#define clr(a) memset(a,0,sizeof(a))
#define clr_minus1(a) memset(a,-1,sizeof(a))
#define clr_INT(a) memset(a,INT,sizeof(a))
#define clr_true(a) memset(a,true,sizeof(a))
#define clr_false(a) memset(a,false,sizeof(a))
#define clr_queue(q) while(!q.empty()) q.pop()
#define clr_stack(s) while(!s.empty()) s.pop()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define dep(i, a, b) for (int i = a; i > b; i--)
#define repin(i, a, b) for (int i = a; i <= b; i++)
#define depin(i, a, b) for (int i = a; i >= b; i--)
#define pi acos(-1.0)
#define eps 1e-6
#define MOD 1000000007
#define MAXN 100100
#define N
#define M 26
char text[MAXN];
int k;
struct Digit
{
    string s;
    int pos;
    friend bool operator<(const Digit a,const Digit b){
        return a.s>b.s;
    }
}d,newd;
priority_queue<Digit>q;
int main()
{
    scanf("%s %d",text,&k);
    int len=len(text);
    rep(i,0,len){
        newd.s=text[i];
        newd.pos=i;
        q.push(newd);
    }
    ll t1=len,t2=k;
    if(t1*(t1+1)/2<t2){
        printf("No such line.\n");
        exit(0);
    }
    int t=0;
    while(!q.empty())
    {
        d=q.top();
        q.pop();
        t+=1;
        if(t==k){
            cout<<d.s<<endl;
            exit(0);
        }
        newd=d;
        newd.pos+=1;
        if(newd.pos<len){
            newd.s+=text[newd.pos];
            q.push(newd);
        }
    }
}






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值