POJ 1743 Musical Theme (字符串HASH+二分)

Musical Theme

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 15900 Accepted: 5494

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

 

对两个相邻的之间的差值进行HASH

二分判断就可以了

 

主要是学习下HASH的方法。

这题用SA也很快

 1 /* ***********************************************
 2 Author        :kuangbin
 3 Created Time  :2013-11-5 17:25:20
 4 File Name     :E:\2013ACM\专题学习\字符串HASH\POJ1743.cpp
 5 ************************************************ */
 6 
 7 #include <stdio.h>
 8 #include <string.h>
 9 #include <iostream>
10 #include <algorithm>
11 #include <vector>
12 #include <queue>
13 #include <set>
14 #include <map>
15 #include <string>
16 #include <math.h>
17 #include <stdlib.h>
18 #include <time.h>
19 using namespace std;
20 const int HASH = 10007;
21 const int MAXN = 20010;
22 struct HASHMAP
23 {
24     int head[HASH],next[MAXN],size;
25     unsigned long long state[MAXN];
26     int f[MAXN];
27     void init()
28     {
29         size = 0;
30         memset(head,-1,sizeof(head));
31     }
32     int insert(unsigned long long val,int _id)
33     {
34         int h = val%HASH;
35         for(int i = head[h]; i != -1;i = next[i])
36             if(val == state[i])
37             {
38                 return f[i];
39             }
40         f[size] = _id;
41         state[size] = val;
42         next[size] = head[h];
43         head[h] = size++;
44         return f[size-1];
45     }
46 };
47 HASHMAP H;
48 const int SEED = 13331;
49 unsigned long long P[MAXN];
50 unsigned long long S[MAXN];
51 int A[MAXN];
52 int n;
53 bool check(int x)
54 {
55     H.init();
56     for(int i = x;i < n;i++)
57         if(H.insert(S[i] - S[i-x]*P[x],i) < i-x)
58             return true;
59     return false;
60 }
61 
62 int main()
63 {
64     //freopen("in.txt","r",stdin);
65     //freopen("out.txt","w",stdout);
66     P[0] = 1;
67     for(int i = 1;i < MAXN;i++)
68         P[i] = P[i-1] * SEED;
69     while(scanf("%d",&n) && n)
70     {
71         for(int i = 1;i <= n;i++)
72             scanf("%d",&A[i]);
73         for(int i = 1;i < n; i++)
74             A[i] = A[i+1] - A[i];
75         S[0] = 0;
76         for(int i = 1;i < n;i++)
77             S[i] = S[i-1]*SEED + A[i];
78         int ans = 0;
79         int l = 4, r = n-1;
80         while(l <= r)
81         {
82             int mid = (l + r)/2;
83             if(check(mid))
84             {
85                 ans = mid;
86                 l = mid+1;
87             }
88             else r = mid-1;
89         }
90         if(ans < 4)ans = -1;
91         ans++;
92         printf("%d\n",ans);
93     }
94     return 0;
95 }

 

 

 

 

 

 

    原文作者:kuangbin
    原文地址: https://www.cnblogs.com/kuangbin/p/3409055.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞