codeforces 128B - String
2016-09-06 22:10
369 查看
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 < 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).
AC代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <queue>
using namespace std;
char c[100001];
int cn;
string s[100001];
int ed[100001];
struct Cmp{
bool operator()(int a, int b){
return s[a] > s[b];
}
};
typedef priority_queue<int, vector<int>, Cmp> pq;
pq pri;
int main() {
while ( scanf("%s", c) != EOF ){
cn = strlen(c);
pq().swap(pri);//将队列清空
for (int ci = 0; ci < cn; ci++){
s[ci] = c[ci];
ed[ci] = ci;
pri.push(ci);
}
int k;
scanf("%d", &k);
for (int ki = 0; ki < k; ki++) {
if ( pri.empty() ) {
puts("No such line.");
break;
}
int t = pri.top();
pri.pop();
if ( ki+1 == k ) puts(s[t].c_str() );
ed[t]++;
if ( ed[t] != cn ){
s[t].push_back(c[ed[t]]);
pri.push(t);
}
}
}
return 0;
}
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 < 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).
AC代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <queue>
using namespace std;
char c[100001];
int cn;
string s[100001];
int ed[100001];
struct Cmp{
bool operator()(int a, int b){
return s[a] > s[b];
}
};
typedef priority_queue<int, vector<int>, Cmp> pq;
pq pri;
int main() {
while ( scanf("%s", c) != EOF ){
cn = strlen(c);
pq().swap(pri);//将队列清空
for (int ci = 0; ci < cn; ci++){
s[ci] = c[ci];
ed[ci] = ci;
pri.push(ci);
}
int k;
scanf("%d", &k);
for (int ki = 0; ki < k; ki++) {
if ( pri.empty() ) {
puts("No such line.");
break;
}
int t = pri.top();
pri.pop();
if ( ki+1 == k ) puts(s[t].c_str() );
ed[t]++;
if ( ed[t] != cn ){
s[t].push_back(c[ed[t]]);
pri.push(t);
}
}
}
return 0;
}
相关文章推荐
- CodeForces 128B - String 优先队列暴力..
- CodeForces 128B String 后缀数组 或 优先队列维护 求第K小子串
- Codeforces 2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest A题 (String模拟)
- Codeforces 828 C String Reconstruction
- codeforces 676-C. Vasya and String(尺取)
- 【Educational Codeforces Round 16】 Codeforces 710E Generate a String
- codeforces 23A. You're Given a String...
- Codeforces 827E Rusty String - 快速傅里叶变换 - 暴力
- CodeForces 23A You're Given a String... (简单题)
- codeforces 877 problem B Nikita and string 【思维 + 前后缀思想】
- codeforces 710E Generate a String [dp]【动态规划】
- codeforces 710E E. Generate a String(dp)
- 【非常高%】【codeforces 733A】Grasshopper And the String
- Playing with String(codeforces 305E)
- CodeForces 623 A. Graph and String(水~)
- Codeforces - AIM Tech Round (Div. 2)C - Graph and String
- 「CodeForces - 598B」Queries on a String
- 【CodeForces】889 B. Restoration of string
- Codeforces-710E Generate a String
- codeforces 709D D. Recover the String(构造)