Stammering Aliens(后缀数组+二分)

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

题意: 先给一个 m 代表某种字串再这个字符串中最少要出现 m 次。再给这个字符串。求出现次数 >= m的最长字串的长度,以及它的起始点。

思路:先用求出后缀数组,再求出height数组。然后我们就可以用二分枚举可能出现的长度,并且通过height数组快速判断  >=这个长度的字串的出现次数,并且维护最左左标(也就是这个字串的起始位置)。

代码如下:

/*
    给你一个字符串,一个m;
    求在这个字符串中重复 >= m 次的字串的最长 长度,以及它的起始位置。
    思路:后缀数组模板题,求出height数组后,二分枚举最长的长度,然后通过height数组
       检查这个字串是否出现了 >= m 次,并且维护最左端点。
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 40010
#define inf 0x3f3f3f3f
using namespace std;
char str[N];
int wa[N],wb[N],wv[N],ws[N];
int cmp(int *r,int a,int b,int l)
{
    return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da(const char r[],int sa[],int n,int m)//求后缀数组
{
    int i,j,p,k;
    int *x=wa,*y=wb,*t;
    for(i=0; i<m; i++)ws[i]=0;
    for(i=0; i<n; i++)ws[x[i]=r[i]]++;
    for(i=1; i<m; i++)ws[i]+=ws[i-1];
    for(i=n-1; i>=0; i--)sa[--ws[x[i]]]=i;

    for(j=1,p=1; p<n; j*=2,m=p)
    {
        for(p=0,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++)ws[i]=0;
        for(i=0; i<n; i++)ws[wv[i]]++;
        for(i=1; i<m; i++)ws[i]+=ws[i-1];
        for(i=n-1; i>=0; i--)sa[--ws[wv[i]]]=y[i];
        t=x;x=y;y=t;
        for(p=1,x[sa[0]]=0,i=1; i<n; i++)
            x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
    }
    return;
}
int sa[N],Rank[N],height[N];
void calheight(const char r[],int sa[],int n)//求height数组
{
    int i,j,k=0;
    for(i=1; i<=n; i++) Rank[sa[i]]=i;
    for(i=0; i<n; height[Rank[i++]]=k)
        for(k?k--:0,j=sa[Rank[i]-1]; r[i+k]==r[j+k]; k++);
    //for(i=n; i>=1; --i) ++sa[i],Rank[i]=Rank[i-1];
}
int check(int mid,int n,int len)
{
    int cnt=-1,ans=0,tmp=-1; //cnt 代表已经有确定n个长度 > mid 的字串,且最左端点是cnt
    for(int i=1; i<len; i++) //ans 代表同一个字串 >mid 的个数,tmp代表可能的最左端点
    {
        if(height[i]<mid) //相同前缀的长度 < mid
        {
            ans=1;  //当作第一个
            tmp=sa[i]; //记录下标
        }
        else  //相同前缀的长度 >= mid
        {
            ans++; //个数 +1
            tmp=max(tmp,sa[i]);//维护最左
        }
        if(ans>=n)//已经找到了m个
            cnt=max(cnt,tmp);//维护最左
    }
    return cnt;
}
int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        scanf("%s",str);
        int len=strlen(str);
        if(n==1)
        {
            printf("%d 0\n",len);
            continue;
        }
        da(str,sa,len+1,130);//先求后缀数组
        calheight(str,sa,len); //再求height数组
        int l=1,r=len,ans;
        while(l<=r) //二分枚举长度
        {
            int mid=(l+r)>>1;
            int temp=check(mid,n,len+1);//获得此长度的最左端点
            if(temp!=-1)//这个长度满足条件
            {
                l=mid+1;
                ans=temp;//记录最左下标
            }
            else //不能满足条件
                r=mid-1;
        }
        if(r>=1) //长度 >=1 说明找到了
            printf("%d %d\n",r,ans);
        else
            printf("none\n");
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41890797/article/details/85041259