首页 > 代码库 > Stammering Aliens

Stammering Aliens

Stammering Aliens
Time Limit: 2000MS Memory Limit: 65536K
   

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

3baaaababababbababbab11baaaababababbababbab3cccccc0

Sample Output

5 12none4 2
分析:求出现至少m次的最长字符子串及最大位置;
   后缀数组+set;(二分hash)

代码:
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <algorithm>#include <climits>#include <cstring>#include <string>#include <set>#include <map>#include <unordered_map>#include <queue>#include <stack>#include <vector>#include <list>#define rep(i,m,n) for(i=m;i<=n;i++)#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)#define mod 1000000007#define inf 0x3f3f3f3f#define vi vector<int>#define pb push_back#define mp make_pair#define fi first#define se second#define ll long long#define pi acos(-1.0)#define pii pair<int,int>#define Lson L, mid, ls[rt]#define Rson mid+1, R, rs[rt]#define sys system("pause")#define freopen freopen("in.txt","r",stdin)const int maxn=4e4+10;using namespace std;ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}inline ll read(){    ll x=0;int f=1;char ch=getchar();    while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}    while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}    return x*f;}int n,m,k,t,cntA[maxn],cntB[maxn],sa[maxn],lev[maxn],height[maxn],A[maxn],B[maxn],tsa[maxn];char ch[maxn];void solve(){    for (int i = 0; i < 256; i ++) cntA[i] = 0;    for (int i = 1; i <= n; i ++) cntA[ch[i]] ++;    for (int i = 1; i < 256; i ++) cntA[i] += cntA[i - 1];    for (int i = n; i; i --) sa[cntA[ch[i]] --] = i;    lev[sa[1]] = 1;    for (int i = 2; i <= n; i ++)    {        lev[sa[i]] = lev[sa[i - 1]];        if (ch[sa[i]] != ch[sa[i - 1]]) lev[sa[i]] ++;    }    for (int l = 1; lev[sa[n]] < n; l <<= 1)    {        for (int i = 0; i <= n; i ++) cntA[i] = 0;        for (int i = 0; i <= n; i ++) cntB[i] = 0;        for (int i = 1; i <= n; i ++)        {            cntA[A[i] = lev[i]] ++;            cntB[B[i] = (i + l <= n) ? lev[i + l] : 0] ++;        }        for (int i = 1; i <= n; i ++) cntB[i] += cntB[i - 1];        for (int i = n; i; i --) tsa[cntB[B[i]] --] = i;        for (int i = 1; i <= n; i ++) cntA[i] += cntA[i - 1];        for (int i = n; i; i --) sa[cntA[A[tsa[i]]] --] = tsa[i];        lev[sa[1]] = 1;        for (int i = 2; i <= n; i ++)        {            lev[sa[i]] = lev[sa[i - 1]];            if (A[sa[i]] != A[sa[i - 1]] || B[sa[i]] != B[sa[i - 1]]) lev[sa[i]] ++;        }    }    for (int i = 1, j = 0; i <= n; i ++)    {        if (j) j --;        while (ch[i + j] == ch[sa[lev[i] - 1] + j]) j ++;        height[lev[i]] = j;    }}multiset<int>p;set<int>q;int main(){    int i,j;    while(~scanf("%d",&m)&&m)    {        scanf("%s",ch+1);        n=strlen(ch+1);        if(m==1)        {            printf("%d %d\n",n,0);            continue;        }        solve();        p.clear(),q.clear();        rep(i,1,m-1)p.insert(height[i]),q.insert(sa[i]);        int ans=0,pos=-1;        rep(i,m,n)        {            p.erase(p.lower_bound(height[i-m+1]));            p.insert(height[i]);            q.insert(sa[i]);            if(ans<=*p.begin())            {                auto x=q.end();                x--;                if(ans<*p.begin()||*x-1>pos)pos=*x-1;                ans=*p.begin();            }            q.erase(sa[i-m+1]);        }        if(ans)printf("%d %d\n",ans,pos);        else puts("none");    }    //system("Pause");    return 0;}

Stammering Aliens