牛客网刷题日记6

 题目

 

例子

示例1
输入
aabb
3
输出
aab
说明
不同的子串依次为:
a aa aab aabb ab abb b bb
所以答案为aab
   
示例2
输入
aaa
3
输出
aaa

题解

#include<bits/stdc++.h>
using namespace std;

int main(){

    string s;
    int k;

    cin>>s>>k;

    set<string> result;//在该容器中本身就是有序的,就是字典序
    int n = s.size();
    string temp;
    for(int i = 0; i< n;i++){
        temp.clear();
        for(int j = i; j< n;j++){
            temp.push_back(s[j]);//字符串也可以压栈构建

            if(result.size()==k){//该轮在往后走已经超出最小字符串了,因为后面字符串肯定还会更长
                if (temp>=*(--result.end())){
                    break;
                }
                //如果不进入,就证明还在前面。
            }

            result.insert(temp);//插入结果。
            //最多存k个
            if(result.size()>k) result.erase(--result.end());
        }
    }


    cout<<*(--result.end())<<endl;

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值