AtCoder Regular Contest #097C: K-th Substring 题解

总共有 N 2 个字符串,每两个字符串比较的复杂度是 O ( n ) ,所以全部拉出来排序的复杂度是 O ( n 3 l o g n ) ,不能通过
但注意到题目的限制条件 k 5 ,我们考虑到一个串的前缀的字典序一定比它小,所以最后答案字符串的长度不会超过5
所以我们把所有长度不超过5的字符串拉出来排序就好,注意去重,复杂度 O ( n 2 l o g n )

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

set<string> ss;
string a[200048],s;
int tot=0,k;

int main ()
{
    cin>>s>>k;
    int i,j,n=int(s.size());
    for (i=0;i<=n-1;i++)
        for (j=1;j<=5;j++)
        {
            if (i+j-1>=n) continue;
            ss.insert(s.substr(i,j));
        }
    for (i=1;i<=k-1;i++) ss.erase(ss.begin());
    cout<<(*(ss.begin()))<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/iceprincess_1968/article/details/80310343