码迷,mamicode.com
首页 > 编程语言 > 详细

FOJ 题目 2075 Substring (后缀数组求出现k次的最小字典序子串)

时间:2015-08-27 23:07:16      阅读:1156      评论:0      收藏:0      [点我收藏+]

标签:

Problem 2075 Substring

Accept: 70    Submit: 236
Time Limit: 1000 mSec    Memory Limit : 65536 KB

技术分享 Problem Description

Given a string, find a substring of it which the original string contains exactly n such substrings.

技术分享 Input

There are several cases. The first line of each case contains an integer n.The second line contains a string, no longer than 100000.

技术分享 Output

If the such substring doesn‘t exist, output "impossible", else output the substring that appeared n times in the original string.If there are multiple solutions, output lexicographic smallest substring.

技术分享 Sample Input

2ababba

技术分享 Sample Output

ac代码
<pre name="code" class="cpp"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA5gAAAA3CAYAAABpauUbAAAgAElEQVR4Ae1dW2xV15n+jpXHBiy1BEUViYmtKqM8lExjAzlmRlhTm15IVV6YSUJCVUxJmzLHvMTM1JEicnvCnpSEBDMTOy00T0kHQibkwU4cTgSGprzQJpJtiKVUYiCa+KFvNXv0r/vee+3jY/tc9vb5jzBn77X/9a9/ff9lXfc6ub/+9a8B6JMDIK/ELacpGBoUl3PnzqGzs5Ptgn1DOkKD+oGKAqnBwPilFoz1suQYZbBkDJeMoTDDBsCPbUUHnGz1FVlvrDcHAY5zBEYV4zX5W3d3t4G8KQjkqDK4FcBcc5rFooFxISthu2AM2A7SEyN15DaxuoHjUyUwYNtOj21XQp/VbK/YVrJnKyZecpzMVF+O9YZMjkG03vT3bRQyglu39D0CsYyZ4zQ0NgZkELfYLtgPGtwP0hcPgVsBxevGjk+V0AvHOGr92Y7KwYBtJau2wv2Ycuy7EvG0suMI1ltW9WYGkwCayCjMzGEQyFkOThMDi0bGhYykkesvZsKr7gczGPmnr+H222/Hd0evCrwrVe5E/+2C7+1fewofVrQeH+Kpr0mZb++fqKjM2uYqhcFK5CfqRLPxFdVpY/LT9qG/2e64/ac2z2cH2kb0t48mrWnVawvSHze0vvR3WnXE8qnVceV/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FL+osbs8nGvhA9q33G+dvyQeeiDu+tZCGMnniQMLVV7oua/OWj5Rl0iZoWdOZTN4WVO/9Ola2IQ74eHYa4Lv+OGP6Khs36mc0mqK5TLE1gNBZzOqbbt8A0zphvGfGFGDTMMnOnBNkFH/XIrJh8CWn5BnpSWnw1biPhNqx0RcdH0zrAVfbPf7pp7Yc3nJsl16wU8kODFAtRmheB0Wo6Z39dBbvB9qB3Gy8g5e0ZipJqBNfyPyPFmHDk+33TZxVT13FSjUnBc/QUP68vF2B7u1VFw99Fayfp620ELp80s5DrE0Ef1E9OqW7R1gRnRaL724cvquo3rLEVFs5ZUTGh6B999/H93d3Q2PAwPACKQJAfbLymmDsawcliudE9tKNjXMemO9ZROBbEod9bdMvYOZTchZakaAEWAEGAFGgBFgBBgBRoARYAQaAwEeYDaGnrmWjAAjwAgwAowAI8AIMAKMACPACFQdAR5gVh1iLoARYAQYAUaAEWAEGAFGgBFgBBiBxkCAB5iNoWeuJSPACDACjAAjwAgwAowAI8AIMAJVR4AHmFWHmAtgBBgBRoARYAQYAUaAEWAEGAFGoDEQ4AFmY+iZa8kIMAKMACPACDACjAAjwAgwAoxA1RHgAWbVIeYCGAFGgBFgBBgBRoARYAQYAUaAEWgMBHL0w5iNUVWuJSPACDACjAAjwAgwAowAI8AIMAKMQKUR6O7uNixzQRDwANPAwReMACPACDACjAAjwAgwAowAI8AIMAJLRYC3yC4VOc7HCDACjAAjwAgwAowAI8AIMAKMACMQQoAHmCE4+IYRYAQYAUaAEWAEGAFGgBFgBBgBRmCpCPAAc6nIcT5GgBFgBBgBRoARYAQYAUaAEWAEGIEQAv8Paeexo7TwDtsAAAAASUVORK5CYII=" alt="" />、
<pre name="code" class="cpp">#include<stdio.h>           
#include<string.h>           
#include<algorithm>           
#include<iostream>          
#define min(a,b) (a>b?b:a)       
#define max(a,b) (a>b?a:b)        
using namespace std;          
char str[103030];        
int sa[103030],Rank[103030],rank2[103030],height[103030],c[103030],*x,*y;      
int n;      
void cmp(int n,int sz)      
{      
    int i;      
    memset(c,0,sizeof(c));      
    for(i=0;i<n;i++)      
        c[x[y[i]]]++;      
    for(i=1;i<sz;i++)      
        c[i]+=c[i-1];      
    for(i=n-1;i>=0;i--)      
        sa[--c[x[y[i]]]]=y[i];      
}      
void build_sa(char *s,int n,int sz)      
{      
    x=Rank,y=rank2;      
    int i,j;      
    for(i=0;i<n;i++)      
        x[i]=s[i],y[i]=i;      
    cmp(n,sz);      
    int len;      
    for(len=1;len<n;len<<=1)      
    {      
        int yid=0;      
        for(i=n-len;i<n;i++)      
        {      
            y[yid++]=i;      
        }      
        for(i=0;i<n;i++)      
            if(sa[i]>=len)      
                y[yid++]=sa[i]-len;      
            cmp(n,sz);      
        swap(x,y);      
        x[sa[0]]=yid=0;      
        for(i=1;i<n;i++)      
        {      
            if(y[sa[i-1]]==y[sa[i]]&&sa[i-1]+len<n&&sa[i]+len<n&&y[sa[i-1]+len]==y[sa[i]+len])      
                x[sa[i]]=yid;      
            else      
                x[sa[i]]=++yid;      
        }      
        sz=yid+1;      
        if(sz>=n)      
            break;      
    }      
    for(i=0;i<n;i++)      
        Rank[i]=x[i];      
}      
void getHeight(char *s,int n)      
{      
    int k=0;      
    for(int i=0;i<n;i++)      
    {      
        if(Rank[i]==0)      
            continue;      
        k=max(0,k-1);      
        int j=sa[Rank[i]-1];      
        while(s[i+k]==s[j+k])      
            k++;      
        height[Rank[i]]=k;      
    }      
}    
int minv[103010][20],lg[103030];      
void init_lg()    
{    
    int i;    
    lg[1]=0;    
    for(i=2;i<102020;i++)    
    {    
        lg[i]=lg[i>>1]+1;    
    }    
}    
void init_RMQ(int n)    
{    
    int i,j,k;    
    for(i=1;i<=n;i++)    
    {    
        minv[i][0]=height[i];    
    }    
    for(j=1;j<=lg[n];j++)      
    {      
        for(k=0;k+(1<<j)-1<=n;k++)      
        {      
            minv[k][j]=min(minv[k][j-1],minv[k+(1<<(j-1))][j-1]);       
        }      
    }    
}    
int lcp(int l,int r)    
{    
    l=Rank[l];    
    r=Rank[r];    
    if(l>r)    
        swap(l,r);    
    l++;    
    int k=lg[r-l+1];    
    return min(minv[l][k],minv[r-(1<<k)+1][k]);      
}   
void solve(int n,int k)
{
	int i,j;
	for(i=1;i<=n;i++)
	{
		if(i+k-1>n)
			break;
		int t=lcp(sa[i],sa[i+k-1]);
		if(t>height[i]&&(i+k>n||t+k<=n&&t>height[i+k]))
		{
			for(j=0;j<t;j++)
			{
				printf("%c",str[j+sa[i]]);
			}
			printf("\n");
			return;
		}
	}
	printf("impossible\n");
}
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		scanf("%s",str);
		int len=strlen(str);
		build_sa(str,len+1,128);
		getHeight(str,len);
		init_lg();
		init_RMQ(len);
		solve(len,n);
	}
}







技术分享 Source

FOJ有奖月赛-2012年3月

版权声明:本文为博主原创文章,未经博主允许不得转载。

FOJ 题目 2075 Substring (后缀数组求出现k次的最小字典序子串)

标签:

原文地址:http://blog.csdn.net/yu_ch_sh/article/details/48036055

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!