码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces 128 B. String 优先队列

时间:2015-08-31 13:36:27      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:优先队列

链接: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。 

但是vs2010 的优先队列好像很慢。 另外一题有次也用到优先队列,vs2010跑好几分钟跑不出来的数据,CB可以秒跑。 所以这题不要交vs2010,不然会超时。


#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;
}





版权声明:本文为博主原创文章,未经博主允许不得转载。

codeforces 128 B. String 优先队列

标签:优先队列

原文地址:http://blog.csdn.net/u013532224/article/details/48132397

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!