poj 1743 后缀数组+二分答案 求一个串的最长无重叠的重复出现次数最多的子串

来源:互联网 发布:gta5捏脸数据女爱丽丝 编辑:程序博客网 时间:2024/06/05 22:54


Description

A musical melody is represented as a sequence of N (1<=N<=20000)notes that are integers in the range 1..88, each representing a key on the piano. It is unfortunate but true that this representation of melodies ignores the notion of musical timing; but, this programming task is about notes and not timings. 
Many composers structure their music around a repeating &qout;theme&qout;, which, being a subsequence of an entire melody, is a sequence of integers in our representation. A subsequence of a melody is a theme if it: 
  • is at least five notes long 
  • appears (potentially transposed -- see below) again somewhere else in the piece of music 
  • is disjoint from (i.e., non-overlapping with) at least one of its other appearance(s)

Transposed means that a constant positive or negative value is added to every note value in the theme subsequence. 
Given a melody, compute the length (number of notes) of the longest theme. 
One second time limit for this problem's solutions! 

Input

The input contains several test cases. The first line of each test case contains the integer N. The following n integers represent the sequence of notes. 
The last test case is followed by one zero. 

Output

For each test case, the output file should contain a single line with a single integer that represents the length of the longest theme. If there are no themes, output 0.

Sample Input

3025 27 30 34 39 45 52 60 69 79 69 60 52 45 39 34 30 26 22 1882 78 74 70 66 67 64 60 65 800

Sample Output

5

找一个长度为K,且不重叠的最长子串。二分答案长度,记录最大和最小下标,然后检测是否符合题意(满足之间的位置大于k,这样就不会重叠)


#include<iostream>#include<cstring>#include<cstdio>#include<cmath>#define inf 0x3f3f3f3f#include<algorithm>using namespace std;const int MAX=20020;int n;int wa[MAX],wb[MAX],wsf[MAX],wv[MAX],sa[MAX];int rank[MAX],height[MAX],r[MAX];int cmp(int *r,int a,int b,int k){    return (r[a]==r[b])&&(r[a+k]==r[b+k]);}void da(int *r,int *sa,int n,int m)//??N????N??1????????????????cmp???{    int i,j,p,*x=wa,*y=wb,*t;    for(i=0;i<m;i++) wsf[i]=0;    for(i=0;i<n;i++) wsf[x[i]=r[i]]++;    for(i=1;i<m;i++) wsf[i]+=wsf[i-1];    for(i=n-1;i>=0;i--) sa[--wsf[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++) wsf[i]=0;        for(i=0;i<n;i++) wsf[wv[i]]++;        for(i=1;i<m;i++) wsf[i]+=wsf[i-1];        for(i=n-1;i>=0;i--) sa[--wsf[wv[i]]]=y[i];        for(t=x,x=y,y=t,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++;    }}void calheight(int *r,int *sa,int n)//??height??,{    int i,j,k=0;    for(i=1;i<=n;i++)        rank[sa[i]]=i; //????1~n?sa[]=(0~n-1)??    for(i=0;i<n;i++)    {        if(k)  k--;        else  k=0;        j=sa[rank[i]-1];        while(r[i+k]==r[j+k])        k++;        height[rank[i]]=k;    }}bool check(int x){    int maxx=sa[1];    int minx=sa[1];    for(int i=2;i<=n;i++)    {        if(height[i]>=x)        {            maxx=max(maxx,sa[i]);            minx=min(minx,sa[i]);            if(maxx-minx>=x)                return 1;        }        else        {            minx=sa[i];            maxx=sa[i];        }    }    return 0;}int main(){    while(cin>>n)    {        if(n==0)            return 0;        int num[20010];        for(int i=0;i<=n-1;i++)            scanf("%d",&num[i]);        for(int i=0;i<=n-2;i++)            r[i]=num[i+1]-num[i]+90;        r[n-1]=0;        n=n-1;        da(r,sa,n+1,200);        calheight(r,sa,n);        int ll=4,rr=n;        while(ll<=rr)        {            int mid=(ll+rr)/2;            if(check(mid))                ll=mid+1;            else                rr=mid-1;        }        if(ll-1<4)            cout<<0<<endl;        else            cout<<ll<<endl;    }}


0 0
原创粉丝点击