"POJ1743" musical Themes song theme suffix array, (can also be done with the hash+ two points)

Source: Internet
Author: User

Test instructions

1829:musical Themes Song Theme Time limit: 1 Sec Memory Limit: MB
Submit: Solved: 15
[Submit] [Status] [Web Board] Description

We use a sequence of n (1 <= n <=5000) notes to represent a piece of music, each note being 1. Integers in the range 88, each representing a key on the piano. Unfortunately, this method of expressing the melody ignores the value of the note, but the programming task is about pitch, regardless of the value.

Many composers build music around a recurring "theme". In our music notation, "subject" is a subsequence of the entire note sequence, which needs to meet the following conditions:

    • A minimum length of 5 notes
    • Repeated in the song (may be transferred, see below)
    • Duplicate occurrences of the same theme cannot overlap

"Transpose" means that each note in the subject sequence is added or subtracted from the same integer value.

Given a piece of music, calculate the length of the longest topic (that is, the number of notes).

Input

The first line of the output file contains the integer n. Each of the following lines (except possibly the last line) contains 20 integers that represent a sequence of notes. The last line may be less than 20 notes.

Output

The output file should contain only one integer, which is the length of the longest topic. If there is no theme in the song, then output 0.

Sample Input3025 ------------------1882Sample Output5HINT

This 5-length theme in the sample is the last 5 notes of the first line in the input file and 5 notes at the beginning of the second line

Source

Usaco

Best solution
C C + + Pascal
Null 80643.wyfcyx
(0ms,1100kb,2168b)
21804.Ares
(72ms,292kb,561b)
[Submit] [Status] [Web Board]  Powered by LLQ. 2012-2014. Changchun Jida University attached to the experimental school.

Exercises

Ah, write a suffix array, and then check the two points according to the height of the group, so that each paragraph to meet the height long enough, sweep again, if the maximum of two is not coincident (sa difference is large enough), return 1 ...


Code:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 21000using namespace Std;int s[n];int sa[n],rank[n],h[n],n,m,len;int cnt[n],val[n],stk[n],_val[n],top;bool issame ( int A,int B,int hl) {return val[a]==val[b]&& ((A+hl>=len&&b+hl>=len) | | (A+hl<len&&b+hl<len&&val[a+hl]==val[b+hl]));} void SA (int lim) {int i,j,k,hl;for (i=0;i<lim;i++) cnt[i]=0;for (i=0;i<len;i++) cnt[val[i]=s[i]]++;for (i=1;i< lim;i++) cnt[i]+=cnt[i-1];for (i=len-1;i>=0;i--) sa[--cnt[val[i]]]=i;for (k=1;; k++) {top=0,hl=1<< (k-1); for (i=0;i<len;i++) if (Sa[i]+hl>=len) stk[top++]=sa[i];for (i=0;i<len;i++) if (SA[I]&GT;=HL) stk[top++]=sa[i]-hl;for (i=0;i<lim;i++) cnt[i]=0;for (i=0;i<len;i++) cnt[val[i]]++;for (i=1;i <lim;i++) cnt[i]+=cnt[i-1];for (i=len-1;i>=0;i--) sa[--cnt[val[stk[i]]]]=stk[i];for (lim=i=0;i<len;lim++) {for (J=i;j<len-1&&issame (SA[J],SA[J+1],HL); j + +); for (; i<=j;i++) _val[sa[i]]=lim;} for (i=0;i<len;i++) val[i]=_val[i];if (Lim==len) break;} for (i=0;i<len;i++) rank[sa[i]]=i;for (k=i=0;i<len;i++) {if (k) k--;if (!rank[i]) continue;while (s[i+k]==s[sa[ RANK[I]-1]+K]) k++;h[rank[i]]=k;}} BOOL Check (int mid) {int l=sa[0],r=sa[0],i;for (i=0;i<len;i++) {if (h[i]<mid) {l=r=sa[i];continue;} L=min (L,sa[i]); R=max (R,sa[i]); if (r-l>mid) return 1;} return 0;} int main () {//freopen ("test.in", "R", stdin), int i,j,k;scanf ("%d", &len), scanf ("%d", &s[0]); for (i=1;i<len; i++) scanf ("%d", &s[i]), S[i-1]=s[i]-s[i-1]+100;sa (n), int l=0,r=len,mid,ans=0;while (l<=r) {if (r-l<=3) { for (i=l;i<=r;i++) if (check (i)) ans=i;break;} Mid=l+r>>1;if (check (mid)) L=mid;else R=mid-1;} if (ans>=4) printf ("%d\n", ans+1), else puts ("0"); return 0;}



"POJ1743" musical Themes song theme suffix array, (can also be done with the hash+ two points)

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.