POJ - 1743 Musical Theme
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.
这题对于刚刚hash入门的我有点懵逼
仔细思考下 还是可以写的
先预处理一下 求出每两个数字之间的值
然后hash 然后在二分枚举串的长度 其实也是对字串进行hash
hash 感觉其实就是一个特定进制(方式下)的映射
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 #include <set> 8 #include <map> 9 #include <string> 10 #include <math.h> 11 #include <stdlib.h> 12 #include <time.h> 13 using namespace std; 14 const int maxn = 20010; 15 const int HASH = 10007; 16 struct hashmap { 17 int head[HASH], next[maxn], siz, f[maxn]; 18 unsigned long long state[maxn]; 19 void init() { 20 siz = 0; 21 memset(head, -1, sizeof(head)); 22 } 23 int add(unsigned long long val, int _id) { 24 int h = val % HASH; 25 for(int i = head[h]; i != -1; i = next[i]) 26 if(val == state[i]) return f[i]; 27 f[siz] = _id; 28 state[siz] = val; 29 next[siz] = head[h]; 30 head[h] = siz++; 31 return f[siz- 1]; 32 } 33 } H; 34 const int seed = 13331; 35 unsigned long long p[maxn], s[maxn]; 36 int a[maxn], n; 37 int check(int x) { 38 H.init(); 39 for (int i = x ; i < n ; i++) 40 if (H.add(s[i] - s[i - x]*p[x], i) < i - x) return 1; 41 return 0; 42 } 43 int main() { 44 p[0] = 1; 45 for (int i = 1 ; i < maxn ; i++) 46 p[i] = p[i - 1] * seed; 47 while(scanf("%d", &n) && n) { 48 for (int i = 1 ; i <= n ; i++) 49 scanf("%d", &a[i]); 50 for (int i = 1 ; i < n ; i++) 51 a[i] = a[i + 1] - a[i]; 52 s[0] = 0; 53 for (int i = 1 ; i < n ; i++) 54 s[i] = s[i - 1] * seed + a[i]; 55 int ans = 0; 56 int low = 4, high = n - 1 ; 57 while(low <= high) { 58 int mid = (low + high) / 2; 59 if (check(mid)) { 60 ans = mid; 61 low = mid + 1; 62 } else high = mid - 1; 63 } 64 if(ans < 4)ans = -1; 65 printf("%d\n",ans+1); 66 } 67 return 0; 68 }