POJ1743 Musical Theme [后缀数组+分组/并查集]

Musical Theme
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 27539   Accepted: 9290

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


不重叠最长重复子串
冷静的说一下做法:
1.差分之后体现的是变化率,解决可以+-问题(就像是波形一样)  【注意原序列相差满足x 差分序列相差满足x+1 因为差分后每个元素代表两个元素,得到的结果长度至少是2 如1 2 3-->1 1不行】
2.不重叠的重复子串,也是二分最长长度然后分组,同一组内height满足了最长长度的要求一组内sa值相差问题(因为sa[i]表示了位置),mx-mn>=k的话可以
 
我靠什么鬼.........................该死该死该死该死什么问题啊!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
难道是后缀数组的问题?不能啊
该死(曾经这里有几十行这个词...)
 
【2016-12-28 00:15:57】
发现问题了,傻逼的对于n=1特判结果后面的一个数没读入..........
实践证明我的模板是对的
 

 【2017-02-06】

除了分组还有一种做法,从大到小枚举L然后合并 维护并查集内mx和mn看看是不是>L(注意并查集用的编号是排序后的排名)  

为什么>L 因为 1 2 3-->1 1 两个其实是重合的

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=2e4+5;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}
int n,m,k;
int s[N];
int sa[N],c[N],t1[N],t2[N],height[N],rnk[N];

void getHeight(){
    int k=0;
    for(int i=1;i<=n;i++) rnk[sa[i]]=i;
    for(int i=1;i<=n;i++){
        if(k) k--;
        if(rnk[i]==1) continue;
        int j=sa[rnk[i]-1];
        while(i+k<=n&&j+k<=n&&s[i+k]==s[j+k]) k++;
        height[rnk[i]]=k;
    }
}
inline bool cmp(int *r,int a,int b,int j){
    return a+j<=n&&b+j<=n&&r[a]==r[b]&&r[a+j]==r[b+j];
}
void getSA(){ 
    m=200;
    int *r=t1,*k=t2;
    for(int i=0;i<=m;i++) c[i]=0;
    for(int i=1;i<=n;i++) c[r[i]=s[i]]++;
    for(int i=1;i<=m;i++) c[i]+=c[i-1];
    for(int i=n;i>=1;i--) sa[c[r[i]]--]=i;

    for(int j=1;j<=n;j<<=1){
        int p=0;
        for(int i=n-j+1;i<=n;i++) k[++p]=i;
        for(int i=1;i<=n;i++) if(sa[i]>j) k[++p]=sa[i]-j;

        for(int i=0;i<=m;i++) c[i]=0;
        for(int i=1;i<=n;i++) c[r[k[i]]]++;
        for(int i=1;i<=m;i++) c[i]+=c[i-1];
        for(int i=n;i>=1;i--) sa[c[r[k[i]]]--]=k[i];

        swap(r,k);p=0;r[sa[1]]=++p;
        for(int i=2;i<=n;i++) r[sa[i]]=cmp(k,sa[i],sa[i-1],j)?p:++p;
        if(p>=n) break;m=p;
    }
}

struct edge{
    int v,ne;
}e[N];
int h[N],cnt;
inline void ins(int u,int v){
    cnt++;
    e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;
}
int fa[N],mn[N],mx[N];
int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
void unn(int x,int y){
    x=find(x);y=find(y);
    if(x!=y){
        fa[x]=y;
        mn[y]=min(mn[y],mn[x]);
        mx[y]=max(mx[y],mx[x]);
    }
}
void solve(){
    cnt=0;memset(h,0,sizeof(h));
    for(int i=1;i<=n;i++){
        fa[i]=i,mn[i]=mx[i]=sa[i];
        ins(height[i],i);
    }
    for(int L=n-1;L>=4;L--){
        for(int i=h[L];i;i=e[i].ne) unn(e[i].v,e[i].v-1);
        if(h[L]){
            int x=find(e[h[L]].v);
            if(mx[x]-mn[x]>L) {printf("%d
",L+1);return;} 
        }
    }
    puts("0");
}
int main(){
    freopen("in","r",stdin);
    while((n=read())){
        n--;
        int last=read(),x;
        for(int i=1;i<=n;i++){
            x=read();
            s[i]=x-last+100;
            last=x;
        }
        getSA();
        getHeight();
        solve();
    }
}
SA+并查集
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=2e4+5;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}
int n,m,k;
int s[N];
int sa[N],c[N],t1[N],t2[N];
inline bool cmp(int *r,int a,int b,int j){
    return a+j<=n&&b+j<=n&&r[a]==r[b]&&r[a+j]==r[b+j];
}
int rnk[N],height[N];
void getHeight(){
    int k=0;
    for(int i=1;i<=n;i++) rnk[sa[i]]=i;
    for(int i=1;i<=n;i++){
        if(k) k--;
        if(rnk[i]==1) continue;
        int j=sa[rnk[i]-1];
        while(i+k<=n&&j+k<=n&&s[i+k]==s[j+k]) k++;
        height[rnk[i]]=k;
    }
}
void buildSA(){
    int *r=t1,*y=t2;
    for(int i=0;i<=m;i++) c[i]=0;
    for(int i=1;i<=n;i++) c[r[i]=s[i]]++;
    for(int i=1;i<=m;i++) c[i]+=c[i-1];
    for(int i=n;i>=1;i--) sa[c[r[i]]--]=i;
    
    for(int j=1;j<=n;j<<=1){
        int p=0;
        for(int i=n-j+1;i<=n;i++) y[++p]=i;
        for(int i=1;i<=n;i++) if(sa[i]>j) y[++p]=sa[i]-j;
        
        for(int i=0;i<=m;i++) c[i]=0;
        for(int i=1;i<=n;i++) c[r[y[i]]]++;
        for(int i=1;i<=m;i++) c[i]+=c[i-1];
        for(int i=n;i>=1;i--) sa[c[r[y[i]]]--]=y[i];
        
        swap(r,y);p=0;r[sa[1]]=++p;
        for(int i=2;i<=n;i++) r[sa[i]]=cmp(y,sa[i],sa[i-1],j)?p:++p;
        if(p>=n) break;m=p;
    }
    getHeight();
}

bool check(int mid){
    int mn=sa[1],mx=sa[1];
    for(int i=2;i<=n;i++){
        if(height[i]>=mid){
            mn=min(mn,sa[i]);
            mx=max(mx,sa[i]);
            if(mx-mn>mid) return true;
        }else mn=mx=sa[i];
    }
    return false;
}
void solve(){
    int l=1,r=n>>1,ans=0;
    while(l<=r){
        int mid=(l+r)>>1;
        if(check(mid)) ans=mid,l=mid+1;
        else r=mid-1;
    }
    if(ans+1<5) puts("0");
    else printf("%d
",ans+1);
}
int main(){
    //freopen("in.txt","r",stdin);
    while((n=read())){
        n--;m=200;
        int last=read(),x;
        for(int i=1;i<=n;i++){
            x=read();
            s[i]=x-last+100;
            last=x;
        }
        buildSA();
        solve();
    }
}
 
 
原文地址:https://www.cnblogs.com/candy99/p/6227659.html