题解 | #第K小子串#
第K小子串
http://www.nowcoder.com/practice/c59d9690061e448fb8ec7d744c20ebff
- 使用set迭代是一种不错的方式。最优效率的方式,其他看注释。
#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;
}大厂笔试题题解 文章被收录于专栏
主要是公司笔试题得一些总结
