Codeforces Beta Round #94 (Div. 1 Only) B.String

B. String
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

One day in the IT lesson Anna and Maria learned about the lexicographic order.

String x is lexicographically less than string y, if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that xi < yi, and for any j (1 ≤ j < ixj = yj. Here |a| denotes the length of the string a. The lexicographic comparison of strings is implemented by operator < in modern programming languages​​.

The teacher gave Anna and Maria homework. She gave them a string of length n. They should write out all substrings of the given string, including the whole initial string, and the equal substrings (for example, one should write out the following substrings from the string "aab": "a", "a", "aa", "ab", "aab", "b"). The resulting strings should be sorted in the lexicographical order. The cunning teacher doesn't want to check all these strings. That's why she said to find only the k-th string from the list. Help Anna and Maria do the homework.

Input

The first line contains a non-empty string that only consists of small Latin letters ("a"-"z"), whose length does not exceed 105. The second line contains the only integer k (1 ≤ k ≤ 105).

Output

Print the string Anna and Maria need — the k-th (in the lexicographical order) substring of the given string. If the total number of substrings is less than k, print a string saying "No such line." (without the quotes).

Examples
input
aa
2
output
a
input
abc
5
output
bc
input
abab
7
output
b
Note

In the second sample before string "bc" follow strings "a", "ab", "abc", "b".

题意是给一个字符串,然后问把所有子串拿出来排序后,第k大的是哪个串,输出这个串,如果不存在就输出题目里那些话。

长度为n的串,有n*(n+1)/2个子串。直接模拟复杂度是至少是o(n²*logn)肯定是不行的,于是想用后缀数组(后来看了题解真的是用优先队列去暴力模拟……真是……,不过我试了一下真的是会超时,可能姿势不够优雅)
后缀数组的话,就是先求出所有sa然后从前往后把前缀填上去,但是注意有些排名在后面的后缀的前缀实际上是在排名在前的后缀的前面的比如
原:abacab
后缀: ab
abacab
ac
注意到实际上后缀abacab的前缀a排名在ab前面,这个地方我们用一下height数组,把所有前缀能向下延伸多少算一下,还要记录一下上一次前缀扩展了多长,不要算重了。
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<time.h>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<iostream>
using namespace std;
#define  LONG long long
const LONG   INF=0x3f3f3f3f;
const LONG    MOD=1e9+7;
const double PI=acos(-1.0);
#define clr0(x) memset(x,0,sizeof x)
#define clrI(x) memset(x,-1,sizeof(x))
#define clr1(x) memset(x,INF,sizeof x)
#define clr2(x) memset(x,-INF,sizeof x)
#define EPS 1e-10
string  s;
int t1[101000],t2[101000],cc[101000],x[101000],sa[101000],Rank[101000],height[101000];
LONG  len;
bool cmp(int *y,int a,int b,int k)
{
	int a1=y[a];
	int b1=y[b];
	int a2=a+k>=len ? -1:y[a+k];
	int b2=b+k>=len ? -1:y[b+k];
	return a1==b1 && a2==b2;
}
int make_sa()
{
	int *x=t1,*y=t2;
	int m=26;
	for(int i=0; i<m; i++) cc[i]=0;
	for(int i=0; i<len; i++) ++cc[x[i]=s[i]-'a'];
	for(int i=1; i<m; i++) cc[i]+=cc[i-1];
	for(int i=len-1; i>=0; i--) sa[--cc[x[i]]]=i;

	for(int k=1; k<=len; k<<=1)
	{
		int p=0;
		for(int i=len-k; i<len; i++) y[p++]=i;
		for(int i=0; i<len; i++)
		   if( sa[i]>=k ) y[p++]=sa[i]-k;

		for(int i=0; i<m; i++) cc[i]=0;
		for(int i=0; i<len; i++) ++cc[x[y[i]]];
		for(int i=1; i<m; i++) cc[i]+=cc[i-1];
		for(int i=len-1; i>=0; i--) sa[--cc[x[y[i]]]]=y[i];

		swap(x,y);
		m=1; x[sa[0]]=0;
		for(int i=1; i<len; i++)
		  x[sa[i]]=cmp(y,sa[i],sa[i-1],k) ? m-1:m++;

		if( m>=len ) break;
	}
}
void make_height()
{
	for(int i=0; i<len; i++) Rank[sa[i]]=i;
	height[0]=0;
	int k=0;
	for(int i=0; i<len; i++)
	{
		if(!Rank[i]) continue;
		int j=sa[Rank[i]-1];
		if(k) k--;
		while(s[i+k]==s[j+k]) k++;
		height[Rank[i]]=k;
	}
}
int main()
{
//    freopen("C:\\Users\\ZhangYuyang\\Desktop\\in.txt","r",stdin);
//freopen("C:\\Users\\ZhangYuyang\\Desktop\\out.txt","w",stdout);
    LONG k ;
    len = s.length();
    if( k > len * (len + 1)/2)
    {
        cout<<"No such line."<<endl;
        return 0;
    }
    make_sa();
	make_height();
    vector<LONG  > vec[30];
    for(int i = 0 ; i< 30 ; ++ i)vec[i].clear();
    for(int i = 0 ;i < len ;++ i)
        vec[s[i]-'a'+ 1].push_back((LONG )i);
    LONG  sum[30] ;
    for(int i =1; i<= 26 ;++ i)
    {
        sum[i] = 0;
        for(int j = 0 ; j< vec[i].size(); ++ j)
            sum[i] += len - vec[i][j] ;
    }
    int id ;
    for(int i =1; i<= 26 ;++ i)
    {
        if(  k <= sum[i])
        {
            id = i ;
            break ;
        }
        k -= sum[i];
    }
    int  p =vec[id].size();
    int pre = 0;
    for(int i = 0 ; i < len ;++ i)
    {
        if(s[sa[i]] != id + 'a' - 1)continue ;
        int now = 1;
        for(int j = sa[i] ; j < len ;++ j)
        {
            int t = j - sa[i] + 1;
            int res = 0;
            if(t > pre )res= 1;
            for(int l = i + 1 ; l < len  ; l ++)
            {
                if( height[l] < t)
                    break ;
                if(t > pre)
                    res ++ ;
                now = max(now , t);
            }
            if( k <= res)
            {
                for(int tt = sa[i] ; tt <= j ; ++ tt)
                    printf("%c",s[tt]);cout<<endl;
                return 0;
            }
            k -= res;
        }
        pre = now  ;
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值