【poj1743】Musical Theme 后缀数组+二分
2016-02-27 17:10
381 查看
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
LouTiancheng@POJ又是后缀数组…
求差分数列的最长重复子串,使得重复的串在原数列中不互相重复,若小于5则输出0。
差分后后缀数组,然后二分答案len,在高度数组中找到连续的一段使得它们的高度都大于len,小于len的可以作为分割线把lcp数组分组,然后记录每组sa的最大和最小值,然后作查看看是否大于len,若大于len则证明存在。是大于而不是大于等于是因为是差分数列。
对拍百万组数据没拍出错,交上去就WA,我放弃了…求解
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int SZ = 1000010; const int INF = 1000000010; int n,lcp[SZ],sa[SZ],rank[SZ],k = 1,tmp[SZ]; bool cmp_sa(int i,int j) { if(rank[i] != rank[j]) return rank[i] < rank[j]; else { int x = i + k <= n ? rank[i + k] : -1; int y = j + k <= n ? rank[j + k] : -1; return x < y; } } void get_sa(int s[]) { for(int i = 0;i <= n;i ++) { sa[i] = i; rank[i] = i == n ? -INF : s[i]; } for(k = 1;k <= n;k <<= 1) { sort(sa,sa + 1 + n,cmp_sa); tmp[sa[0]] = 0; for(int i = 1;i <= n;i ++) tmp[sa[i]] = tmp[sa[i - 1]] + (cmp_sa(sa[i - 1],sa[i]) ? 1 : 0); for(int i = 0;i <= n;i ++) rank[i] = tmp[i]; } } void get_lcp(int s[]) { int h = 0; for(int i = 0;i <= n;i ++) rank[sa[i]] = i; lcp[0] = 0; for(int i = 0;i < n;i ++) { int j = sa[rank[i] - 1]; if(h) h --; while(i + h < n && j + h < n) { if(s[i + h] == s[j + h]) h ++; else break; } lcp[rank[i] - 1] = h; } } bool check(int len) { int minsa = sa[0],maxsa = sa[0]; for(int i = 1;i < n - 1;i ++) { if(lcp[i] >= len) { minsa = min(minsa,sa[i]); maxsa = max(maxsa,sa[i]); // minsa = min(minsa,sa[i + 1]); // maxsa = max(maxsa,sa[i + 1]); if(maxsa - minsa > len) {return true;} } else minsa = maxsa = 0; } return false; } int div() { int l = 0,r = n,ans; while(l <= r) { int mid = (l + r) >> 1; if(check(mid)) ans = mid,l = mid + 1; else r = mid - 1; } return ans; } int s[SZ]; int main() { freopen("poj1743.in","r",stdin); freopen("poj1743.out","w",stdout); while(~scanf("%d",&n) && n) { // init(); for(int i = 0;i < n;i ++) scanf("%d",&s[i]); if(n <= 5) { puts("0"); continue; } for(int i = 0;i < n - 1;i ++) s[i] = s[i + 1] - s[i] + 100; n --; s = 0; get_sa(s); get_lcp(s); /* for(int i = 0;i <= n;i ++) printf("%d ",s[i]); puts(""); for(int i = 0;i <= n;i ++) printf("%d ",sa[i]); puts(""); for(int i = 0;i <= n;i ++) printf("%d ",rank[i]); puts(""); for(int i = 0;i <= n;i ++) printf("%d ",lcp[i]); puts("");*/ int ans = div(); printf("%d\n",ans < 4 ? 0 : ans + 1); } return 0; } /* 8 1 1 2 1 1 1 1 2 11 1 1 1 1 1 1 1 1 1 1 1 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 */
2017.9.7更新
二分检查那边写挂了。因为LCP是考虑的两个串,断开时需要把断点代表的串也算进去。
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> using namespace std; typedef long long LL; const int SZ = 1000010; const int INF = 1000000010; LL read() { LL n = 0; char a = getchar(); int flag = 0; while(a < '0' || a > '9') { if(a == '-') flag = 1; a = getchar(); } while(a >= '0' && a <= '9') n = n * 10 + a - '0',a = getchar(); if(flag) n = -n; return n; } int lcp[SZ],sa[SZ],rank[SZ],len; bool cmp(int *y,int a,int b,int k) { int a1 = y[a],b1 = y[b]; int a2 = a + k >= len ? -1 : y[a + k]; int b2 = b + k >= len ? -1 : y[b + k]; return a1 == b1 && a2 == b2; } int t1[SZ],t2[SZ],cc[SZ]; void get_sa(int s[]) { int *x = t1,*y = t2,m = 200; for(int i = 0;i < m;i ++) cc[i] = 0; for(int i = 0;i < len;i ++) ++ cc[x[i] = s[i]]; for(int i = 1;i < m;i ++) cc[i] += cc[i - 1]; for(int i = len - 1;~i;i --) sa[-- cc[x[i]]] = i; for(int k = 1;k < len;k <<= 1) { int p = 0; for(int i = len - k;i < len;i ++) y[p ++] = i; for(int i = 0;i < len;i ++) if(sa[i] >= k) y[p ++] = sa[i] - k; for(int i = 0;i < m;i ++) cc[i] = 0; for(int i = 0;i < len;i ++) ++ cc[x[y[i]]]; for(int i = 1;i < m;i ++) cc[i] += cc[i - 1]; for(int i = len - 1;~i;i --) sa[-- cc[x[y[i]]]] = y[i]; swap(x,y); m = 1; x[sa[0]] = 0; for(int i = 1;i < len;i ++) x[sa[i]] = cmp(y,sa[i - 1],sa[i],k) ? m - 1 : m ++; if(m >= len) break; } } void get_lcp(int s[]) { for(int i = 0;i < len;i ++) rank[sa[i]] = i; int h = 0; lcp[0] = 0; for(int i = 0;i < len;i ++) { if(!rank[i]) continue; int j = sa[rank[i] - 1]; if(h) h --; while(s[i + h] == s[j + h]) h ++; lcp[rank[i]] = h; } } int k; bool check(int d) { int minn = sa[0],maxn = sa[0]; for(int i = 1;i < len;i ++) { if(lcp[i] >= d) { minn = min(minn,sa[i]); maxn = max(maxn,sa[i]); if(maxn - minn > d) return true; } else minn = maxn = sa[i]; } return false; } int div() { int l = 0,r = len + 1; while(r - l > 1) { int mid = (l + r) >> 1; if(check(mid)) l = mid; else r = mid; } return l; } int s[SZ]; int main() { while(~scanf("%d",&len) && len) { for(int i = 0;i < len;i ++) s[i] = read(); if(len <= 5) { puts("0"); continue; } for(int i = 0;i < len - 1;i ++) s[i] = s[i + 1] - s[i] + 100; len --; s[len] = 0; get_sa(s); get_lcp(s); /* for(int i = 0;i < len;i ++) printf("%d ",s[i]); puts(""); for(int i = 0;i < len;i ++) printf("%d ",sa[i]); puts(""); for(int i = 0;i < len;i ++) printf("%d ",lcp[i]); puts(""); */ int ans = div(); printf("%d\n",ans < 4 ? 0 : ans + 1); } return 0; }
相关文章推荐
- 配置_Druid和Spring关联监控配置
- Android性能优化之一:ViewStub
- 学习Servlet的重要应用 在什么地方用写路径
- Find The Multiple
- 前端开发必备
- Exynos4412裸机开发 —— UART
- npm 淘宝镜像配置一直无效的可能原因
- 深入解析Android App开发中Context的用法
- Hello World
- IOS开发--UI进阶之iCarousel学习(待翻译)
- Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake
- android多语言切换
- 通过汇编一个简单的C程序,分析汇编代码理解计算机是如何工作的
- 道客巴巴免费复制
- 线程结束的正确方式
- Java集群--大型网站是怎样解决多用户高并发访问的
- io流处理文件夹复制功能(java代码)
- springMVC对异常处理的支持
- Linux精讲——chattr特殊权限
- springMVC对异常处理的支持