后缀数组模板poj1743(找不重叠的两个相同子串

Musical Theme

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 35108   Accepted: 11648

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

30
25 27 30 34 39 45 52 60 69 79 69 60 52 45 39 34 30 26 22 18
82 78 74 70 66 67 64 60 65 80
0

Sample Output

5

Hint

Use scanf instead of cin to reduce the read time.

Source

LouTiancheng@POJ

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 200010
using namespace std;
int s[maxn];
int sa[maxn];//sa[i]=j表示第i名后缀从j开始
int rank[maxn];//排第几名,与sa相逆
int height[maxn];//sa[i-1]后缀与sa[i]后缀的最大公共前缀
int t1[maxn];
int t2[maxn];
int c[maxn];//c[i] 小于等于i的数目用来处理名次
int n;
void build(int m)
{
    int i,*x=t1,*y=t2;
    for(int i=0;i<m;i++)
        c[i]=0;
    for(int i=0;i<n;i++)
        c[x[i]=s[i]]++;//x[i]第i个字符的绝对值,想想基数排序的图,相当于变成数字那个过程;
        for(int i=1;i<m;i++)
            c[i]+=c[i-1];//获得小于等于i的个数
        for(int i=n-1;i>=0;i--)
        sa[--c[x[i]]]=i;//处理名次获得每个位置的sa

    for(int k=1;k<=n;k<<=1)
    {
        int p=0;//第二关键字排名
        for(i=n-k;i<n;i++)
            y[p++]=i;//y内容表示第一关键字位置后面没出现最大。
        for(i=0;i<n;i++)
            if(sa[i]>=k)
            y[p++]=sa[i]-k;//第一关键字位置等于第二关键字位置-k
        for(i=0;i<m;i++)
            c[i]=0;
        for(i=0;i<n;i++)
            c[x[y[i]]]++;
        for(i=1;i<m;i++)
            c[i]+=c[i-1];
        for(i=n-1;i>=0;i--)
            sa[--c[x[y[i]]]]=y[i];//重新计算名次
        swap(x,y);//y表示名次数组
        p=1;//x中不同数的个数
        x[sa[0]]=0;//添加的0始终第0名
        for(i=1;i<n;i++)
            x[sa[i]]=y[sa[i]]==y[sa[i-1]]&&y[sa[i]+k]==y[sa[i-1]+k]?p-1:p++;//统计不同的个数
        if(p>=n)
            break;
        m=p;
    }
}
void buildheight()
{
    int i,j,k=0;
    for(i=0;i<n;i++)
        rank[sa[i]]=i;//找名次
    for(int i=0;i<n;i++)
    {
        if(k)
            k--;
        j=sa[rank[i]-1];
        while(s[i+k]==s[j+k])
            k++;//找最长公共前缀
        height[rank[i]]=k;
    }
}
bool check(int k)
{
    int minv=sa[1];
    int maxv=sa[1];
    for(int i=2;i<n;i++)
    {if(height[i]>=k)
       {

       maxv=max(maxv,sa[i]);
        minv=min(minv,sa[i]);
        if(maxv-minv>=k)
            return true;
       }
    else
        maxv=minv=sa[i];
}
return false;
}
int main()
{
while(~scanf("%d",&n)&&n)
{
    int j,k;
    scanf("%d",&j);
    for(int i=0;i<n-1;i++)
    {
        scanf("%d",&k);
        s[i]=k-j+100;
        j=k;
    }
    if(n<9)
    {printf("0\n");
    continue;
}
n=n+1;
s[n-1]=0;
build(200);
buildheight();
if(check(4)==false)
{
    printf("0\n");
    continue;
}
    int l=1,r=n/2;
    while(l<r)
    {
        int m=l+(r-l+1)/2;
        if(check(m))
            l=m;
        else
            r=m-1;

    }
    printf("%d\n",l+1);
}
}

猜你喜欢

转载自blog.csdn.net/sdauguanweihong/article/details/84071319