1. 程式人生 > >Stammering Aliens(字尾陣列+二分)

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");
    }
}