Codeforces B. String Priority Queue

Source: Internet
Author: User

Links: Http://codeforces.com/contest/128/problem/B

B. StringTime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

The IT lesson Anna and Maria learned about the lexicographic order.

Stringxis lexicographically less than stringy, if eitherxis a prefix ofy(and x? ≠? y ), or there exists suchI(1?≤? i? ≤? min (| x|,?| y|)), that xi? <? y I , and for anyJ(1?≤? J. < I ) xj? =? y J . Here| a|Denotes the length of the stringa. 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 lengthN. They should write out all substrings of the given string, including the whole initial string, and the equal substrings (fo R example, one should write out the following substrings from the string "AaB": "a", "a", "AA", "AB", "AaB", "b"). The resulting strings should is sorted in the lexicographical order. The cunning teacher doesn ' t want to check all these strings. That's why she's said to findk-th string from the list. Help Anna and Maria do the homework.

Input

The first line contains a non-empty string that is only consists of small Latin letters ("a"-"Z"), whose length does not exceedTen5 . The second line contains the only integerk(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 are less than K, print a string saying "No such line." (without the quotes).

Sample Test (s) input
Aa2
Output
A
Input
Abc5
Output
Bc
Input
Abab7
Output
B
Note

In the second sample before string "BC" Follow strings "a", "ab", "ABC", "b".



Test instructions: Dictionary ordering substring to find the nth size in the substring.

Practice: You can use a priority queue because you find up to the 100,000th largest. First put in a single. Then constantly remove the smallest, then add a letter after it, and then put it back in the queue. Len in the queue, up to n times, so the complexity of log (len) *n.

But vs2010 's priority queue seems to be slow. Another topic also used in the priority queue, vs2010 run for several minutes to run out of data, CB can run seconds. So do not pay vs2010, otherwise it will time out.


#pragma COMMENT (linker, "/stack:102400000,102400000") #include <stdio.h> #include <stdlib.h> #include < string.h> #include <limits.h> #include <malloc.h> #include <ctype.h> #include <math.h># Include <string> #include <iostream> #include <algorithm>using namespace std; #include <stack># Include <queue> #include <vector> #include <deque> #include <set> #include <map>struct Point{int id;string Ss;bool operator < (const point &b) Const{return ss>b.ss;}}; Char Str[100010];int main () {int n;scanf ("%s%d", str,&n);//n>>str>>n;priority_queue<point>q; int Len=strlen (str); for (int. i=0;str[i];i++) {point tem;tem.ss= ""; Tem.ss=str[i];tem.id=i;q.push (TEM);} int Id=0;point ans;while (!q.empty ()) {point tem=q.top (); Q.pop (); id++;if (id==n) {ans=tem;break;} Tem.id++;if (Tem.id<len) {Tem.ss+=str[tem.id];q.push (TEM);}} if (id!=n) {puts ("No such line.");} Else{cout<<ans.ss<<endl;} return 0;}





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforces B. String Priority Queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.