uva12206 - Stammering Aliens 哈希LCP和后缀数组两种做法

Dr. Ellie Arroway has established contact with an extraterrestrialcivilization. However, all efforts to decode their messages have failed so farbecause, as luck would have it, they have stumbled upon a race of stutteringaliens! Her team has found out that, in every long enough message, the mostimportant words appear repeated a certain number of times as a sequence ofconsecutive characters, even in the middle of other words. Furthermore,sometimes they use contractions in an obscure manner. For example, if theyneed to say bab twice, they might just send the messagebabab, which hasbeen abbreviated because the secondb of the first word can be reused as thefirst b of the second one.

Thus, the message contains possibly overlapping repetitions of the same wordsover and over again. As a result, Ellie turns to you, S.R. Hadden, for help inidentifying the gist of the message.

Given an integer m, and a string s, representing the message, your task isto find the longest substring ofs that appears at least m times. Forexample, in the messagebaaaababababbababbab, the length-5 wordbabab iscontained 3 times, namely at positions5, 7 and 12 (where indices startat zero). No substring appearing3 or more times is longer (see the firstexample from the sample input). On the other hand, no substring appears11times or more (see example 2).

In case there are several solutions, the substring with the rightmostoccurrence is preferred (see example3).

Input 

The input contains several test cases. Each test case consists of aline with an integer m ( m$ \ge$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 possible starting position of such asubstring.

Sample Input 

3
baaaababababbababbab
11
baaaababababbababbab
3
cccccc
0

Sample Output 

5 12
none
4 2

  给一个串,找出至少出现m次的最长子串,如果存在多个,输出最后那个串的位置。

  基于HASH值的LCP算法:为每个后缀计算一个HASH值,任取x,H[i]=H[i+1]x+s[i](0<=i<n,H[n]=0)。H[i]=s[n-1]*x^(n-1-i)+s[n-2]*x(n-2-i)+...+s[i+1]*x+s[i]。HASH(i,L)=H(i)-H(i+L)*x^L。因此HASH(i,L)=s[i+L-1]*x^(L-1)+s[i+L-2]*x(L-2)+...+s[i+1]*x+s[i]。这样就构造出一个s[i]~s[i+L-1]和L有关的字符串HASH值,用unsigned long long,自然溢出,相当于取模2^64。可能会冲突,但概率很小。

  那么用HASH做这个题只需要二分串的长度L,对所有长度为L的串按HASH值排序,HASH相等的时候按串的位置排,位置靠后的排后面,这样HASH值相等的串也就是同样的串都排到一起了,如果大于等于M个,就更新最后位置。复杂度O(logN*logN*N)。


  用后缀数组做的话复杂度是O(logN*N)。构造完后缀数组和height后,也是分组的思想,二分串的长度L,同一组的公共前缀长度大于等于L(但如果一组只有一个后缀的时候不一定满足长度大于等于L)。最后再根据而分出的L求最后位置,特别要注意的是当R-L==1的时候不一定满足长度大于等于L,所以还要判一下。在满足R-L>=M的时候更新最后位置就行。


HASH:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<utility>
#include<cstring>
#include<stack>
#include<queue>
#include<map>
#include<deque>
#include<cmath>
#include<map>
#define INF 0x3f3f3f3f
#define eps 1e-3
using namespace std;
typedef long long LL;
const int MAXN=40010;
const int x=123;
int M,n;
char str[MAXN];
int rank[MAXN];
unsigned long long H[MAXN],xp[MAXN],hash[MAXN];
void hash_init(){
    H[n]=0;
    for(int i=n-1;i>=0;i--) H[i]=H[i+1]*x+str[i]-'a';
    xp[0]=1;
    for(int i=1;i<=n;i++) xp[i]=xp[i-1]*x;
}
bool cmp(const int& a,const int& b){
    return hash[a]<hash[b]||(hash[a]==hash[b]&&a<b);
}
int possible(int L){
    int cnt,pos=-1;
    for(int i=0;i<n-L+1;i++){
        rank[i]=i;
        hash[i]=H[i]-H[i+L]*xp[L];
    }
    sort(rank,rank+n-L+1,cmp);
    for(int i=0;i<n-L+1;i++){
        if(i==0||hash[rank[i]]!=hash[rank[i-1]]) cnt=0;
        if(++cnt>=M) pos=max(pos,rank[i]);
    }
    return pos;
}
int main(){
    freopen("in.txt","r",stdin);
    while(scanf("%d",&M)!=EOF&&M){
        scanf("%s",str);
        n=strlen(str);
        hash_init();
        if(possible(1)==-1){
            printf("none\n");
            continue;
        }
        int L=1,R=n+1;
        while(L+1<R){
            int mid=L+(R-L)/2;
            if(possible(mid)!=-1) L=mid;
            else R=mid;
        }
        int pos=possible(L);
        printf("%d %d\n",L,pos);
    }
    return 0;
}

后缀数组:

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
#define INF 0x3f3f3f3f
#define eps 1e-9
#define MAXNODE 105
#define MOD 10000007
#define SIGMA_SIZE 4
typedef long long LL;
using namespace std;

const int MAXN=40010;
const int MAXM=110;

int M,ans,pos;
char str[MAXN];

struct SuffixArray{
    int s[MAXN];
    int sa[MAXN];
    int rank[MAXN];
    int height[MAXN];
    int c[MAXN];
    int t[MAXN],t2[MAXN];
    int n;

    void clear(){
        n=0;
        memset(sa,0,sizeof(sa));
    }

    void build_sa(int m){
        int i,*x=t,*y=t2;
        for(i=0;i<m;i++) c[i]=0;
        for(int i=0;i<n;i++) c[x[i]=s[i]]++;
        for(int i=1;i<m;i++) c[i]+=c[i-1];
        for(int i=n-1;i>=0;i--) sa[--c[x[i]]]=i;
        for(int k=1;k<=n;k<<=1){
            int p=0;
            for(int i=n-k;i<n;i++) y[p++]=i;
            for(int i=0;i<n;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
            for(int i=0;i<m;i++) c[i]=0;
            for(int i=0;i<n;i++) c[x[y[i]]]++;
            for(int i=1;i<m;i++) c[i]+=c[i-1];
            for(int i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];
            swap(x,y);
            p=1;
            x[sa[0]]=0;
            for(int i=1;i<n;i++) x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;
            if(p>=n) break;
            m=p;
        }
    }

    void build_height(){
        int k=0;
        for(int i=0;i<n;i++) rank[sa[i]]=i;
        height[0]=0;
        for(int i=0;i<n-1;i++){
            if(k) k--;
            int j=sa[rank[i]-1];
            while(s[i+k]==s[j+k]) k++;
            height[rank[i]]=k;
        }
    }
}sa;

bool possible(int len){
    int L=1;
    for(int R=2;R<=sa.n;R++) if(R==sa.n||sa.height[R]<len){
        if(R-L>=M) return true;
        L=R;
    }
    return false;
}

int calp(){
    int L=1,ret=0,maxp=0,MIN=INF;
    for(int R=2;R<=sa.n;R++){
        maxp=max(maxp,sa.sa[R-1]);
        if(R==sa.n||sa.height[R]<ans){
            if(R-L==1){
                if(sa.n-sa.sa[L]-1>=ans&&R-L>=M) ret=max(ret,maxp);
            }
            else if(R-L>=M) ret=max(ret,maxp);
            maxp=0;
            MIN=INF;
            L=R;
        }
    }
    return ret;
}

bool solve(int len){
    if(!possible(1)) return false;
    int L=1,R=len+1;
    while(L+1<R){
        int mid=L+(R-L)/2;
        if(possible(mid)) L=mid;
        else R=mid;
    }
    ans=L;
    pos=calp();
    return true;
}

int main(){
    freopen("in.txt","r",stdin);
    while(scanf("%d",&M)!=EOF&&M){
        sa.clear();
        scanf("%s",str);
        int len=strlen(str);
        for(int i=0;i<len;i++) sa.s[sa.n++]=str[i]-'a'+1;
        sa.s[sa.n++]=0;
        sa.build_sa(30);
        sa.build_height();
        if(!solve(len)) printf("none\n");
        else printf("%d %d\n",ans,pos);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值