poj 1743 后缀数组+二分答案 求一个串的最长无重叠的重复出现次数最多的子串
来源:互联网 发布:gta5捏脸数据女爱丽丝 编辑:程序博客网 时间:2024/06/05 22:54
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!
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.
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
3025 27 30 34 39 45 52 60 69 79 69 60 52 45 39 34 30 26 22 1882 78 74 70 66 67 64 60 65 800
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; }}
0 0
- poj 1743 后缀数组+二分答案 求一个串的最长无重叠的重复出现次数最多的子串
- POJ3261 求指定出现次数的可重叠的最长重复子串(后缀数组)
- 后缀数组(重复次数最多的最长重复子串)—— POJ 3693
- PKU 1743(求最长重复但不重叠子串,后缀数组 + 二分枚举答案)
- Maximum repetition substring+POj+后缀数组之求重复次数最多的连续重复子串
- POJ - 3693 Maximum repetition substring(后缀数组求重复次数最多的连续重复子串)
- PKU3261(Milk Patterns)求可重叠k次的最长重复子串(后缀数组+二分)
- POJ 3261 Milk Patterns ( 后缀数组 + 二分 可重叠的 k 次最长重复子串 )
- 求一个串中出现的第一个最长可重叠重复子串 [后缀数组解法]
- 求一个串中出现的第一个最长可重叠重复子串 [后缀数组解法]
- POJ 3261 - Milk Patterns 求指定重复次数的最长子串,后缀数组+二分 or 线段树
- POJ 1743 Musical Theme (后缀数组加二分求不可重叠最长重复子串)
- 【后缀数组求可重叠的k 次最长重复子串】POJ 3261
- poj-3261 后缀数组求可重叠的k次最长重复子串
- POJ 3261 后缀数组:求可重叠的最长重复子串
- POJ - 3261 Milk Patterns (后缀数组求可重叠的 k 次最长重复子串)
- poj 3261(后缀数组,求可重叠的k次最长重复子串)
- UVA 题目11512 - GATTACA(后缀数组求出现次数最多的子串及重复次数)
- Java内存模型
- Qt第三方库QCustomPlot——认识图表的各个部分
- 适用于智能电网的组合公钥密码体制研究
- 【JZOJ4824】配对游戏
- 抽象类
- poj 1743 后缀数组+二分答案 求一个串的最长无重叠的重复出现次数最多的子串
- Tomcat介绍
- rand()&srand()
- tjut 5918
- Python中的变量
- cordova 插件的使用说明
- 【JZOJ4859】【NOIP2016提高A组集训第7场11.4】连锁店
- 堆排列
- 《OpenCV3.0 编程入门》书中3.1.9节图像的载入、显示与输出示例程序练习报异常问题