SICAU-OJ: 第k小

第k小

题意:

给出一个长度不超过5000的字符串,然后让你找出第K小的字串(1<=K<=5)。重复的串大小相等。

题解:

这里我们知道某些串的前缀是肯定小于等于其本身的。

那么长度为5的串的前缀,肯定依次是最小,第二小....第五小。

所以因为这里1<=K<=5,我们便可以用一个set来维护所有长度不超过5的子串。要用点贪心的思想去解决(每次从最小的那个字符开始选子串)

代码如下:

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

const int N = 5005;
char s[N];
set <char> tmp;
set <string> S;
int k;
int main(){
    scanf("%s",s);
    scanf("%d",&k);
    int len =strlen(s);
    for(int i=0;i<len;i++) tmp.insert(s[i]);
    while(1){
        auto it = tmp.begin();
        auto now = *it;
        for(int i=0;i<len;i++){
            if(s[i]==now){
                char c[N];int tot=0;;
                for(int j=i;j<len;j++){
                    c[tot++]=s[j];
                    c[tot]='