POJ1743 --- Musical Theme (suffix array + binary), suffix Array

Source: Internet
Author: User

POJ1743 --- Musical Theme (suffix array + binary), suffix Array

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.
Descricomposers 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 longappears (potentially transposed -- see below) again somewhere else in the piece of musicis 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 shoshould 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 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

Find the longest child string that cannot overlap, suffix array + binary solution
First, the input numbers are divided into two pairs, and then the suffix array is created.

/*************************************** * *********************************> File Name: POJ1743.cpp> Author: ALex> Mail: zchao1995@gmail.com> Created Time: ******************************** **************************************** /# include <functional> # include <algorithm> # include <iostream> # include <fstream> # include <cstring> # include <cstdio> # include <cmath> # include <cstd Lib> # include <queue> # include <stack> # include <map> # include <bitset> # include <set> # include <vector> using namespace std; const double pi = acos (-1.0); const int inf = 0x3f3f3f3f; const double eps = 1e-15; typedef long LL; typedef pair <int, int> PLL; class SuffixArray {public: static const int N = 20010; int init [N]; int X [N]; int Y [N]; int Rank [N]; int sa [N]; int height [N]; int buc [N]; int size; voi D clear () {size = 0;} void insert (int n) {init [size ++] = n;} bool cmp (int * r, int a, int B, int l) {return (r [a] = r [B] & r [a + l] = r [B + l]);} void getsa (int m = 256) {init [size] = 0; int l, p, * x = X, * y = Y, n = size + 1; for (int I = 0; I <m; ++ I) {buc [I] = 0;} for (int I = 0; I <n; ++ I) {buc [x [I] = init [I] ++ ;}for (int I = 1; I <m; ++ I) {buc [I] + = buc [I-1];} for (int I = N-1; I> = 0; -- I) {sa [-- buc [x [I] = I;} for (l = 1, p = 1; l <= n; m = p, l * = 2) {p = 0; for (int I = n-l; I <n; ++ I) {y [p ++] = I ;}for (int I = 0; I <n; ++ I) {if (sa [I]> = l) {y [p ++] = sa [I]-l ;}} for (int I = 0; I <m; ++ I) {buc [I] = 0 ;}for (int I = 0; I <n; ++ I) {++ buc [x [y [I];} for (int I = 1; I <m; ++ I) {buc [I] + = buc [I-1];} for (int I = n-1; i> = 0; -- I) {s A [-- buc [x [y [I] = y [I];} int I; for (swap (x, y ), x [sa [0] = 0, p = 1, I = 1; I <n; ++ I) {x [sa [I] = cmp (y, sa [I-1], sa [I], l )? P-1: p ++;} if (p> = n) {break ;}}void getheight () {int h = 0; for (int I = 0; I <= size; ++ I) {Rank [sa [I] = I;} height [0] = 0; for (int I = 0; I <size; ++ I) {if (h> 0) {-- h;} int j = sa [Rank [I]-1]; (; I + h <size & j + h <size & init [I + h] = init [j + h]; ++ h ); height [Rank [I]-1] = h ;}} bool judge (int k) {int maxs = sa [1], mins = sa [1]; for (int I = 1; I <size; ++ I) {if (height [I] <k) {maxs = mins = sa [I + 1];} else {maxs = max (maxs, sa [I + 1]); mins = min (mins, sa [I + 1]); if (maxs-mins> k) {return 1 ;}}} return 0;} void solve () {int l = 1, r = size; int mid; int ans = 0; while (l <= r) {int mid = (l + r)> 1; if (judge (mid) {l = mid + 1; ans = mid ;} else {r = mid-1 ;}}++ ans; printf ("% d \ n", ans >=5? Ans: 0) ;}} SA; int val [20010]; int main () {int n; while (~ Scanf ("% d", & n), n) {SA. clear (); for (int I = 1; I <= n; ++ I) {scanf ("% d", & val [I]);} for (int I = n; I> = 2; -- I) {val [I] = val [I]-val [I-1] + 90 ;} for (int I = 2; I <= n; ++ I) {SA. insert (val [I]);} SA. getsa (); SA. getheight (); SA. solve ();} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.