CodeForces 128 B.String(优先队列)

Description
给出一个长度为n的字符串,问该字符串的所有子串中(可重复)字典序第k小的
Input
第一行一个只由小写字母组成的字符串s,第二行一个整数k表示查询s的字典序第k小的子串(1<=|s|,k<=1e5)
Output
输出s的字典序第k小的子串
Sample Input
aa
Sample Output
2
Solution
先把s的每个字符都加到优先队列中,然后从队列中拿出一个串把这个串在原串后的下一个字符加到这个串后面后再加入到优先队列里,一直到拿到第k个串即为答案
Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define maxn 111111
struct node
{
    string s;
    int next;
    bool operator<(const node&b)const
    {
        return s>b.s;
    }
};
char s[maxn];
int k;
int main()
{
    scanf("%s",s);
    scanf("%d",&k);
    int n=strlen(s);
    if(k>1ll*n*(n+1)/2)printf("No such line.\n");
    else
    {
        priority_queue<node>que;
        for(int i=0;i<n;i++)
        {
            node temp;
            temp.s="";
            temp.s=s[i];
            temp.next=i+1;
            que.push(temp);
        }
        node now;
        while(!que.empty())
        {
            now=que.top();
            que.pop();
            k--;
            if(!k)
            {
                cout<<now.s<<endl;
                break;
            }
            if(now.next!=n)
            {
                now.s+=s[now.next];
                now.next++;
                que.push(now);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值