poj1743:后缀数组+二分(1/8个男人,楼教主的男人八题)

代码

给师弟讲课的时候讲了后缀数组,然后决定来A一题..

Musical Theme
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 26043 Accepted: 8803

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

楼教主男人八题之一!!11激动
题意:
求出最长的不重叠重复子串
注意:
答案<5时为0
n要减回来
n = 1特判
(然而n<10直接输出0即可...)

做了两三天,前两天无限RE,一怒之下生成100+M数据,无RE!然后向Zayin大牛求助,就开大了C数组,一语中的,AC...

我是思博..

AC代码:


/*Source Code
Problem: 1743 User: aclolicon
Memory: 1580K Time: 266MS
Language: G++ Result: Accepted
Source Code
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define MAXN 100050
using namespace std;
int n, s[MAXN], t1[MAXN], t2[MAXN], c[MAXN], sa[MAXN], height[MAXN], rank[MAXN];
void buildsa(){
n++;
int m = 600;
int *x = t1, *y = t2;
memset(c, 0, sizeof(c));
for (int i = 0; i < n; i++) c[x[i] = s[i]]++;
for (int i = 1; i < m; i++) c[i] += c[i - 1];
for (int i = n - 1; i >= 0; i--) sa[--c[x[i]]] = i;
for (int k = 1; k <= n; k <<= 1){
int p = 0;
for (int i = n - k; i < n; i++) y[p++] = i;
for (int i = 0; i < n; i++) if (sa[i] >= k) y[p++] = sa[i] - k;
memset(c, 0, sizeof(c));
for (int i = 0; i < n; i++) c[x[y[i]]]++;
for (int i = 1; i < m; i++) c[i] += c[i - 1];
for (int i = n - 1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
swap(x, y);
p = 1;
x[sa[0]] = 0;
for (int i = 1; i < n; i++) x[sa[i]] = (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k]) ? p - 1: p++;
if (p >= n) break;
m = p;
}
}

void getheight(){
for (int i = 0; i < n; i++) rank[sa[i]] = i;
int h = 0;
for (int i = 0; i < n; i++){
int j = sa[rank[i] - 1];
if (h) h--;
while(s[i + h] == s[j + h]) h++;
height[rank[i]] = h;
}
}

bool findx(int k){
int minn, maxn;
minn = maxn = sa[0];
for (int i = 0; i < n; i++){
if (height[i] < k){
minn = maxn = sa[i];
continue;
}
maxn = max(maxn, sa[i]);
minn = min(minn, sa[i]);
if (maxn - minn > k) return 1;
}
return 0;
}

int main(){
while(~scanf("%d", &n)){
if (n == 0) break;
for (int i = 0; i < n; i++) scanf("%d", &s[i]);
if (n <= 10){
printf("0\n");
continue;
}
n--;
for (int i = 0; i < n; i++) s[i] = s[i + 1] - s[i] + 200;
s[n] = 0;
buildsa();
getheight();
int l = 0, r = n;
while(l < r){
int mid = (l + r + 1) >> 1;
if (findx(mid)) l = mid;
else r = mid - 1;
}
printf("%d\n", l < 4? 0: l + 1);
}
}


那么我就是1/8个男人了...

我还没有学会写个人说明!

发表评论

电子邮件地址不会被公开。 必填项已用*标注