POJ 1743 Musical Theme(字尾陣列)
題目大意:給出n個數字。首先將這n個數前後做差,得到另一個長度是n-1的序列。求出這個序列的最長重複子串,且這些子串不能重疊。
PS:這題論文上有解析。
解題思路:先二分答案,把題目變成判斷性問題:判斷是否存在兩個長度為k的字串是否相同的,且不重疊。解決這個問題的關鍵還是利用height陣列。把排序後的字尾陣列分成若干組,其中每組的字尾之間的height值都不小於k。
容易看出,有希望成為最長公共字首不小於k的兩個字尾一定在同一組。然後對於每組字尾,只須判斷每個字尾的sa值的最大值和最小值之差是否不小於k。如果有一組滿足,則說明存在,否則不存在。整個做法的時間複雜度為O(nlogn)
論文上寫的很詳細了啊,不過需要注意細節,比如二分的邊界,以及字串的長度等等。。。
Musical Theme
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 19851 | Accepted: 6779 |
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
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.
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <ctime>
#include <map>
#include <set>
#define eps 1e-9
///#define M 1000100
///#define LL __int64
#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)
#define mod 1000000007
#define Read() freopen("autocomplete.in","r",stdin)
#define Write() freopen("autocomplete.out","w",stdout)
#define Cin() ios::sync_with_stdio(false)
using namespace std;
inline int read()
{
char ch;
bool flag = false;
int a = 0;
while(!((((ch = getchar()) >= '0') && (ch <= '9')) || (ch == '-')));
if(ch != '-')
{
a *= 10;
a += ch - '0';
}
else
{
flag = true;
}
while(((ch = getchar()) >= '0') && (ch <= '9'))
{
a *= 10;
a += ch - '0';
}
if(flag)
{
a = -a;
}
return a;
}
void write(int a)
{
if(a < 0)
{
putchar('-');
a = -a;
}
if(a >= 10)
{
write(a / 10);
}
putchar(a % 10 + '0');
}
const int maxn = 20010;
int wa[maxn], wb[maxn], wv[maxn], ws1[maxn];
int sa[maxn];
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;
for(i = 0; i<m; i++) ws1[i]=0;
for(i = 0; i<n; i++) ws1[x[i]=r[i]]++;
for(i = 1; i<m; i++) ws1[i]+=ws1[i-1];
for(i = n-1; i>=0; i--) sa[--ws1[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++) ws1[i]=0;
for(i = 0; i<n; i++) ws1[wv[i]]++;
for(i = 1; i<m; i++) ws1[i]+=ws1[i-1];
for(i = n-1; i>=0; i--) sa[--ws1[wv[i]]]=y[i];
for(swap(x,y),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;
}
int rank[maxn],height[maxn];
void calheight(int *r,int *sa,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++);
return;
}
int sqe[maxn];
bool judge(int mid, int n)
{
int Max, Min;
for(int i = 2; i <= n; i++)
{
Max = sa[i-1];
Min = sa[i-1];
while(height[i] >= mid)
{
Max = max(sa[i], Max);
Min = min(sa[i], Min);
i++;
}
if(Max - Min > mid) return true;
}
return false;
}
int main()
{
int n;
Cin();
while(cin >>n)
{
if(!n) break;
for(int i = 0; i < n; i++) cin >>sqe[i];
if(n <= 9)
{
puts("0");
continue;
}
for(int i = 0; i < n-1; i++) sqe[i] = sqe[i+1]-sqe[i]+88;
sqe[n-1] = 0;
da(sqe, sa, n, 200);
calheight(sqe, sa, n-1);
int l = 4;
int r = n / 2;
int ans = -1;
while(l <= r)
{
int mid = (l+r)>>1;
if(judge(mid, n))
{
l = mid+1;
ans = mid;
}
else r = mid-1;
}
cout<<ans+1<<endl;
}
return 0;
}
相關文章
- POJ1743 Musical Theme(字尾陣列 二分)陣列
- POJ 1743 Musical Theme (字尾陣列,求最長不重疊重複子串)陣列
- POJ 3581-Sequence(字尾陣列)陣列
- POJ2774Long Long Message(字尾陣列模板)陣列
- 字尾陣列模板陣列
- 字尾陣列 SA陣列
- 字尾陣列,SA陣列
- POJ 2217-Secretary(字尾陣列+高度陣列-最大公共子串長度)陣列
- 字尾陣列複習陣列
- 字尾陣列(後續)陣列
- 字尾陣列詳解陣列
- 【筆記】字尾陣列筆記陣列
- POJ 3415 Common Substrings(字尾陣列求重複字串)陣列字串
- [POJ 3415] Common Substrings (字尾陣列+單調棧優化)陣列優化
- poj 2774 求兩字串的最長公共子串 字尾陣列字串陣列
- OI loves Algorithm——字尾陣列Go陣列
- POJ 2774-Long Long Message(字尾陣列+高度陣列-最大公共子串長度)陣列
- POJ 3693 Maximum repetition substring(字尾陣列求最長重複子串)陣列
- POJ 3294 Life Forms(字尾陣列求k個串的最長子串)ORM陣列
- 學習筆記----字尾陣列筆記陣列
- 字尾陣列 學習筆記陣列筆記
- 字尾陣列學習筆記陣列筆記
- POJ 2752+KMP+利用next陣列性質求出所有相同的字首和字尾KMP陣列
- BZOJ2882: 工藝(字尾陣列)陣列
- POJ 3415-Common Substrings(字尾陣列+單調棧-公共子串的長度)陣列
- poj 2481 樹狀陣列陣列
- P10469 字尾陣列(Hash+二分)陣列
- 【資料結構與演算法】字串匹配(字尾陣列)資料結構演算法字串匹配陣列
- Codeforces #123D: 字尾陣列+單調棧3D陣列
- SPOJ 694 求一個字串有多少子串 字尾陣列字串陣列
- ural 1297 最長迴文子串 字尾陣列陣列
- 洛谷P5108 仰望半月的夜空(字尾陣列)陣列
- SPOJ 687. Repeats(字尾陣列求最長重複子串)陣列
- URAL 1297. Palindrome(字尾陣列求最大回文串)陣列
- HDU5147 Sequence II(樹狀陣列+字首和+字尾和)陣列
- BZOJ 4650 [Noi2016]優秀的拆分:字尾陣列陣列
- SPOJ 694. Distinct Substrings,705. New Distinct Substrings(字尾陣列)陣列
- POJ 3928 Ping pong(樹狀陣列)陣列