Language:
Musical Theme
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:
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长度大于5字符串本身或这个字符串所有字符加上c或减c在字符串中至少出现一次求这个子串最大长度
dc3
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define inf 0x3f3f3f3f
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
using namespace std;
const int Max=400010;
int wa[Max],wb[Max],ws[Max],wv[Max],sa[Max];
int rank[Max],height[Max];
int num1[Max],num[Max];
int MIN(int a,int b){
return a<b?a:b;
}
int MAX(int a,int b){
return a>b?a:b;
}
int c0(int *r,int a,int b){
return r[a]==r[b]&&r[a+1]==r[b+1]&&r[a+2]==r[b+2];
}
int c12(int k,int *r,int a,int b){
if(k==2)return r[a]<r[b]||r[a]==r[b]&&c12(1,r,a+1,b+1);
else return r[a]<r[b]||r[a]==r[b]&&wv[a+1]<wv[b+1];
}
void sort(int *r,int *a,int *b,int n,int m){
int i;
for(i=0;i<n;++i)
wv[i]=r[a[i]];
for(i=0;i<m;++i)
ws[i]=0;
for(i=0;i<n;++i)
ws[wv[i]]++;
for(i=1;i<m;++i)
ws[i]+=ws[i-1];
for(i=n-1;i>=0;--i)
b[--ws[wv[i]]]=a[i];
return ;
}
void dc3(int *r,int *sa,int n,int m)
{
int i,j,*rn=r+n,*san=sa+n,ta=0,tb=(n+1)/3,tbc=0,p;
r[n]=r[n+1]=0;
for(i=0;i<n;++i){
if(i%3!=0)
wa[tbc++]=i;
}
sort(r+2,wa,wb,tbc,m);
sort(r+1,wb,wa,tbc,m);
sort(r,wa,wb,tbc,m);
for(p=1,rn[F(wb[0])]=0,i=1;i<tbc;i++)
rn[F(wb[i])]=c0(r,wb[i-1],wb[i])?p-1:p++;
if(p<tbc)
dc3(rn,san,tbc,p);
else {
for(i=0;i<tbc;++i)
san[rn[i]]=i;
}
for(i=0;i<tbc;++i){
if(san[i]<tb)
wb[ta++]=san[i]*3;
}
if(n%3==1)
wb[ta++]=n-1;
sort(r,wb,wa,ta,m);
for(i=0;i<tbc;++i)
wv[wb[i]=G(san[i])]=i;
for(i=0,j=0,p=0;i<ta && j<tbc;++p)
sa[p]=c12(wb[j]%3,r,wa[i],wb[j])?wa[i++]:wb[j++];
for(;i<ta;++p)
sa[p]=wa[i++];
for(;j<tbc;++p)
sa[p]=wb[j++];
return;
}
void getHeight(int *r,int n){
int i, j, k = 0;
for(i=1;i<=n;++i) rank[sa[i]] = i;
for(i=0;i<n;height[rank[i++]]=k){
for(k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);
}
}bool judge(int k,int n){
int min=inf,max=0;
for(int i=2;i<=n;++i){
if(height[i]>=k){
min=MIN(min,sa[i]);
max=MAX(max,sa[i]);
if(max-min>k)return true;
}
else{
min=max=sa[i];
}
}
return false;
}
int main()
{
int i,j,n;
while(scanf("%d",&n),n){
int min=0;
for(i=1;i<=n;++i){
scanf("%d",&num1[i]);
if(i!=1){
num[i-2]=num1[i]-num1[i-1];
min=MIN(min,num[i-2]);
}
}n--;min--;
int max=0;
for(i=0;i<n;++i){
num[i]-=min;
max=MAX(num[i],max);
}
num[n]=0;
dc3(num,sa,n+1,max+1);
getHeight(num,n);
int l=0,r=n/2+1;
while(l+1<r){
int mid=(l+r)>>1;
if(judge(mid,n))l=mid;
else r=mid;
}
if(l<4)
printf("0\n");
else
printf("%d\n",l+1);
}
return 0;
}
倍增
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define inf 0x3f3f3f3f
using namespace std;
const int Max=400010;
int wa[Max],wb[Max],ws[Max],wv[Max],sa[Max];
int rank[Max],height[Max];
int num[Max];
int MIN(int a,int b){
return a<b?a:b;
}
int MAX(int a,int b){
return a>b?a:b;
}
int cmp(int *r,int a,int b,int l){
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da(int *r,int *sa,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=0;i<m;++i) ws[i]=0;
for(i=0;i<n;++i) ws[x[i]=r[i]]++;
for(i=1;i<m;++i) ws[i]+=ws[i-1];
for(i=n-1;i>=0;--i) sa[--ws[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) ws[i]=0;
for(i=0;i<n;++i) ws[wv[i]]++;
for(i=1;i<m;++i) ws[i]+=ws[i-1];
for(i=n-1;i>=0;--i) sa[--ws[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++;
}
return;
}
void getHeight(int *r,int n){
int i, j, k = 0;
for(i=1;i<=n;++i) rank[sa[i]] = i;
for(i=0;i<n;height[rank[i++]]=k){
for(k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);
}
}bool judge(int k,int n){
int min=inf,max=0;
for(int i=2;i<=n;++i){
if(height[i]>=k){
min=MIN(min,sa[i]);
max=MAX(max,sa[i]);
if(max-min>k)return true;
}
else{
min=max=sa[i];
}
}
return false;
}
int main()
{
int i,j,n;
while(scanf("%d",&n),n){
int min=0;
for(i=1;i<=n;++i){
scanf("%d",&num[i]);
if(i!=1){
num[i-2]=num[i]-num[i-1];
min=MIN(min,num[i-2]);
}
}n--;min--;
int max=0;
for(i=0;i<n;++i){
num[i]-=min;
max=MAX(num[i],max);
}
num[n]=0;
da(num,sa,n+1,max+1);
getHeight(num,n);
int l=0,r=(n>>1)+1;
while(l+1<r){
int mid=(l+r)>>1;
if(judge(mid,n))l=mid;
else r=mid;
}
if(l<4)
printf("0\n");
else
printf("%d\n",l+1);
}
return 0;
}