POJ3882 Stammering Aliens 后缀数组+二分

统计出现m次的重复字串最长的长度和最右边的一个字符串的开始位置。可重叠

很明显的后缀的公共前缀长度问题,high数组的应用。


Stammering Aliens
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 422 Accepted: 153

Description

Dr. Ellie Arroway has established contact with an extraterrestrial civilization. However, all efforts to decode their messages have failed so far because, as luck would have it, they have stumbled upon a race of stuttering aliens! Her team has found out that, in every long enough message, the most important words appear repeated a certain number of times as a sequence of consecutive characters, even in the middle of other words. Furthermore, sometimes they use contractions in an obscure manner. For example, if they need to say bab twice, they might just send the message babab, which has been abbreviated because the second b of the first word can be reused as the first b of the second one. 
Thus, the message contains possibly overlapping repetitions of the same words over and over again. As a result, Ellie turns to you, S.R. Hadden, for help in identifying the gist of the message. 
Given an integer m, and a string s, representing the message, your task is to find the longest substring of s that appears at least m times. For example, in the message baaaababababbababbab, the length-5 word babab is contained 3 times, namely at positions 5, 7 and 12 (where indices start at zero). No substring appearing 3 or more times is longer (see the first example from the sample input). On the other hand, no substring appears 11 times or more (see example 2). In case there are several solutions, the substring with the rightmost occurrence is preferred (see example 3).

Input

The input contains several test cases. Each test case consists of a line with an integer m (m >= 1), the minimum number of repetitions, followed by a line containing a string s of length between m and 40 000, inclusive. All characters in s are lowercase characters from "a" to "z". The last test case is denoted by m = 0 and must not be processed.

Output

Print one line of output for each test case. If there is no solution, output none; otherwise, print two integers in a line, separated by a space. The first integer denotes the maximum length of a substring appearing at least m times; the second integer gives the rightmost starting position of this substring.

Sample Input

3
baaaababababbababbab
11
baaaababababbababbab
3
cccccc
0

Sample Output

5 12
none
4 2

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>

using namespace std;

#define MAXN 50000
char s[MAXN];
int rank[MAXN],sa[MAXN],wa[MAXN],wb[MAXN],high[MAXN],str[MAXN],wss[MAXN],wv[MAXN];
int n,m;

void calhigh(int *str,int *sa,int n)
{
	int i,j,k=0;
	for(i=1;i<=n;i++) rank[sa[i]]=i;
	for(i=0;i<n;high[rank[i++]]=k)
		for(k?k--:0,j=sa[rank[i]-1];str[i+k]==str[j+k];k++);
}

bool cmp(int *r,int a,int b,int l)
{
	return (r[a]==r[b]&&r[a+l]==r[b+l]);
}

void suffix(int *str,int *sa,int n,int m=256)
{
	int i,j,p,*x =wa,*y=wb;
	for(i=0;i<m;i++) wss[i]=0;
	for(i=0;i<n;i++) wss[x[i]=str[i]]++;
	for(i=1;i<m;i++) wss[i]+=wss[i-1];
	for(i=n-1;i>=0;i--) sa[--wss[x[i]]]=i;
	for(j=1,p=1;p<n;m=p,j*=2)
	{
		p=0;
		for(i=n-j;i<n;i++) y[p++]=i;
		for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
		for(i=0;i<n;i++) wv[i]=x[y[i]];
		for(i=0;i<m;i++) wss[i]=0;
		for(i=0;i<n;i++) wss[wv[i]]++;
		for(i=1;i<m;i++) wss[i]+=wss[i-1];
		for(i=n-1;i>=0;i--) sa[--wss[wv[i]]]=y[i];
		for(swap(x,y),x[sa[0]]=0,i=1,p=1;i<n;i++)
			x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
	}
	calhigh(str,sa,n-1);
}

int check(int lth)
{
    int cnt=0,st=-1,tst=-1;
    for(int i=2;i<=n;i++)
    {
        if(high[i]<lth)
        {
            if(cnt>=m-1)
            {
                st=max(st,tst);
                tst=-1;
                cnt=0;
            }
            else
            {
                cnt=0;
                tst=-1;
            }
        }
        else
        {
            cnt++;
            tst=max(tst,max(sa[i-1],sa[i]));
        }
    }
    if(cnt>=m-1) st=max(st,tst);
    return st;

}

int main()
{
    while(~scanf("%d",&m)&&m)
    {
        scanf("%s",s);
        n=strlen(s);
        if(m==1)
        {
            printf("%d 0\n",n);
            continue;
        }
        for(int i=0;i<n;i++) str[i]=s[i];
        str[n]=0;
        suffix(str,sa,n+1);
        int ed,maxs=0,tmp;
        int l=0,r=n,mid;
        while(l<r)
        {
            mid=(l+r)>>1;
            if((tmp=check(mid))!=-1)
            {
                l=mid+1;
                maxs=mid;
                ed=tmp;
            }
            else
                r=mid;
        }
        if(maxs==0) printf("none\n");
        else printf("%d %d\n",maxs,ed);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值