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+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; 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=;
for(int i=;i<=n;i++) rnk[sa[i]]=i;
for(int i=;i<=n;i++){
if(k) k--;
if(rnk[i]==) continue;
int j=sa[rnk[i]-];
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=;
int *r=t1,*k=t2;
for(int i=;i<=m;i++) c[i]=;
for(int i=;i<=n;i++) c[r[i]=s[i]]++;
for(int i=;i<=m;i++) c[i]+=c[i-];
for(int i=n;i>=;i--) sa[c[r[i]]--]=i; for(int j=;j<=n;j<<=){
int p=;
for(int i=n-j+;i<=n;i++) k[++p]=i;
for(int i=;i<=n;i++) if(sa[i]>j) k[++p]=sa[i]-j; for(int i=;i<=m;i++) c[i]=;
for(int i=;i<=n;i++) c[r[k[i]]]++;
for(int i=;i<=m;i++) c[i]+=c[i-];
for(int i=n;i>=;i--) sa[c[r[k[i]]]--]=k[i]; swap(r,k);p=;r[sa[]]=++p;
for(int i=;i<=n;i++) r[sa[i]]=cmp(k,sa[i],sa[i-],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=;memset(h,,sizeof(h));
for(int i=;i<=n;i++){
fa[i]=i,mn[i]=mx[i]=sa[i];
ins(height[i],i);
}
for(int L=n-;L>=;L--){
for(int i=h[L];i;i=e[i].ne) unn(e[i].v,e[i].v-);
if(h[L]){
int x=find(e[h[L]].v);
if(mx[x]-mn[x]>L) {printf("%d\n",L+);return;}
}
}
puts("");
}
int main(){
freopen("in","r",stdin);
while((n=read())){
n--;
int last=read(),x;
for(int i=;i<=n;i++){
x=read();
s[i]=x-last+;
last=x;
}
getSA();
getHeight();
solve();
}
}

SA+并查集

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=2e4+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; 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=;
for(int i=;i<=n;i++) rnk[sa[i]]=i;
for(int i=;i<=n;i++){
if(k) k--;
if(rnk[i]==) continue;
int j=sa[rnk[i]-];
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=;i<=m;i++) c[i]=;
for(int i=;i<=n;i++) c[r[i]=s[i]]++;
for(int i=;i<=m;i++) c[i]+=c[i-];
for(int i=n;i>=;i--) sa[c[r[i]]--]=i; for(int j=;j<=n;j<<=){
int p=;
for(int i=n-j+;i<=n;i++) y[++p]=i;
for(int i=;i<=n;i++) if(sa[i]>j) y[++p]=sa[i]-j; for(int i=;i<=m;i++) c[i]=;
for(int i=;i<=n;i++) c[r[y[i]]]++;
for(int i=;i<=m;i++) c[i]+=c[i-];
for(int i=n;i>=;i--) sa[c[r[y[i]]]--]=y[i]; swap(r,y);p=;r[sa[]]=++p;
for(int i=;i<=n;i++) r[sa[i]]=cmp(y,sa[i],sa[i-],j)?p:++p;
if(p>=n) break;m=p;
}
getHeight();
} bool check(int mid){
int mn=sa[],mx=sa[];
for(int i=;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=,r=n>>,ans=;
while(l<=r){
int mid=(l+r)>>;
if(check(mid)) ans=mid,l=mid+;
else r=mid-;
}
if(ans+<) puts("");
else printf("%d\n",ans+);
}
int main(){
//freopen("in.txt","r",stdin);
while((n=read())){
n--;m=;
int last=read(),x;
for(int i=;i<=n;i++){
x=read();
s[i]=x-last+;
last=x;
}
buildSA();
solve();
}
}
 
 
上一篇:linux中脚本的一些小知识的积累


下一篇:[转载]windows 7 IIS 7.5 ASP.Net 文件上传大小限制