Codeforces 128B. String(优先队列,模仿dijkstra求最短路)
2014-11-04 21:21
405 查看
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 < i) xj = 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
output
input
output
input
output
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);
}
}
}
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 < i) xj = 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);
}
}
}
相关文章推荐
- 【原创】最短路模板 Floyd,优先队列优化dijkstra,SPFA
- hdu-2544-最短路(Dijkstra + Dijkstra优先队列 + Bellman-ford + SPFA +Floyd) 纯模板题
- 蓝桥杯 算法训练 最短路 【SPFA队列优化 + dijkstra优先队列优化】
- dijkstra 优先队列最短路模板
- Codeforces Round #287 (Div. 2) E. Breaking Good [Dijkstra 最短路 优先队列]
- usaco2.4 comehome 最短路 Floyd, Dijkstra, Dijkstra优先队列优化
- HDU ~ 2544 ~ 最短路 (Dijkstra模板,常规版 and 优先队列优化版)
- HDOJ 4318 —— dijkstra + 优先队列 求最短路
- POJ1724 ROADS(深搜DFS,最短路,dijkstra,用优先队列优化)
- ACM:最短路,dijkstra,邻接表的建立,使用邻接表跟优先队列的dijkstra,Bellman-Ford,Floyd。。
- 最短路练习10/poj/1511 Invitation Cards ,(两次spfa),(单源最短路,优先队列优化的Dijkstra)
- hdu 2066最短路 dijkstra 及其优先队列优化
- uva 11374 最短路+记录路径 好题 dijkstra优先队列优化算法 邻接表法 可做模板 G++提交
- 最短路--dijkstra+优先队列优化模板
- hdu2544 最短路 dijkstra的使用优先队列优化的比较
- POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)
- Codeforces 449B Jzzhu and Cities【最短路SPFA+思维+玄学优先队列】
- Dijkstra优先队列优化
- 优先队列优化Dijkstra-hdu2066
- PAT 甲级 1003. Emergency 使用 优先队列 dijkstra 算法