题目
例子
示例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;
}