题意:
给你一个数组,求最长重复不重叠子串, 一段子串 如果每个元素加上或减去某个数 也是相同的。
思路:
虽然每个元素加上或减去某个数也是一样的, 那么这一段相邻的差值是不变的, 那么我们就可以求 差值数组的后缀数组。
这样 就可以直接用后缀数组来做了,论文里的例题。
二分长度k, 然后给sa 数组进行分块。
每一块中height必须都要大于等于k
那么每一块中 的最大sa - 最小sa 如果大于k 的话, 就可以组成不重复的。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define Siz(x) (int)x.size()
using namespace std;
const int maxn = 20000 + 10;
int sa[maxn], lcp[maxn], Rank[maxn], s[maxn], tmp[maxn];
int n,k;
vector<int>Bit[maxn];
bool cmp(int i,int j){
if (Rank[i] != Rank[j]) return Rank[i] < Rank[j];
else {
int ri = i + k <= n ? Rank[i+k] :-1;
int rj = j + k <= n ? Rank[j+k] :-1;
return ri < rj;
}
}
void build_sa(int* s, int* sa){
for (int i = 0; i <= n; ++i){
sa[i] = i;
Rank[i] = i < n ? s[i] : -1;
}
for (k = 1; k <= n; k <<= 1){
sort(sa, sa+1+n, cmp);
tmp[sa[0] ] = 0;
for (int i = 1; i <=n ;++i){
tmp[sa[i] ] = tmp[sa[i-1] ] + (cmp(sa[i-1], sa[i]) ? 1 : 0);
}
for (int i = 0; i <= n; ++i){
Rank[i] = tmp[i];
}
}
}
int get_lcp(int* s, int *sa, int* lcp){
for (int i = 0; i <= n; ++i) Rank[sa[i] ] = i;
int h = 0;
lcp[0] = 0;
for (int i = 0; i < n; ++i){
int j = sa[Rank[i]-1 ];
if (h > 0) --h;
for (; h + j < n && i + h < n; ++h){
if (s[j+h] != s[i+h])break;
}
lcp[Rank[i]-1 ] = h;
}
}
bool judge(int m){
for (int i = 0; i < maxn; ++i) Bit[i].clear();
int cnt = 0;
Bit[0].push_back(sa[1]);
for (int i = 1; i < n; ++i){
if (lcp[i] >= m){
Bit[cnt].push_back(sa[i+1]);
}
else {
Bit[++cnt].push_back(sa[i+1]);
}
}
for (int i = 0; i <= cnt; ++i){
if (Siz(Bit[i]) <= 1) continue;
int mx = -1;
int mi = 0x3f3f3f3f;
for (int j = 0; j < Bit[i].size(); ++j){
int v = Bit[i][j];
if (v > mx) mx = v;
if (v < mi) mi = v;
}
if (mi + m < mx) return true;
}
return false;
}
void init(){
memset(tmp,0,sizeof tmp);
memset(Rank,0,sizeof Rank);
memset(sa,0,sizeof sa);
memset(lcp,0,sizeof lcp);
}
int main(){
while(~scanf("%d",&n) && n){
init();
for (int i = 0; i < n; ++i){
scanf("%d",s+i);
}
for (int i = 1; i < n; ++i) s[i-1] = s[i] - s[i-1] + 100;
s[--n] = 0;
build_sa(s, sa);
get_lcp(s, sa, lcp);
int l = 1, r = n, m;
while(l <= r){
m = l + r >> 1;
if (judge(m)) l = m+1;
else r = m-1;
}
if (r < 4) puts("0");
else printf("%d\n",r+1);
}
return 0;
}
/**
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
5
11
1 1 1 1 1 1 1 1 1 1 1
**/
Musical Theme
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 28994 | Accepted: 9763 |
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.
Source