poj 1743 后缀数组+二分答案 求一个串的最长无重叠的重复出现次数最多的子串
2016-11-04 19:30
513 查看
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
Sample Output
找一个长度为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;
}
}
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
找一个长度为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;
}
}
相关文章推荐
- poj 1743 Musical Theme 求不可重叠最长重复子串(二分答案+height分组)
- POJ1743 Musical Theme,后缀数组,最长重复不重叠子串,二分检索答案
- poj1743 Musical Theme(后缀数组--不可重叠最长重复子串+二分)
- POJ 1743 Musical Theme (后缀数组加二分求不可重叠最长重复子串)
- PKU 1743(求最长重复但不重叠子串,后缀数组 + 二分枚举答案)
- [POJ]1743 不可重叠最长重复字串 二分答案
- poj1743 后缀数组求不可重叠的重复出现的子串最长长度
- POJ 1743 Musical Theme(SA 求最长不可重叠重复子串)
- PKU1743(Musical Theme)求不可重叠最长重复子串(后缀数组+二分)
- poj 1743 字符串 后缀数组 不可重叠最长重复子串
- poj 1743 最长不重叠重复子串 后缀数组+lcp+二分
- POJ 1743 Musical Theme (后缀数组,求最长不重叠重复子串)
- poj 1743 男人八题之后缀数组求最长不可重叠最长重复子串
- POJ 1743 Musical Theme(后缀数组求不可重叠最长重复子串)
- POJ 1743 最长重复子串(不可重叠)
- 后缀数组+最长公共前缀(LCS)+最长重复出现的不重叠字串(POJ 1743)
- poj 3261 Milk Patterns 最长的出现最少k次的重复(可重叠)子串 后缀数组
- poj 1743 二分答案+后缀数组 求不重叠的最长重复子串
- poj 1743 Musical Theme (后缀数组 不可重叠最长重复子串)
- POJ 3261 - Milk Patterns 求指定重复次数的最长子串,后缀数组+二分 or 线段树