Hihocoder #1407: suffix array two • Repeat Melody 2

Source: Internet
Author: User

#1407: suffix array two • Repeat Melody 2Time limit:5000mscase time limit:1000msmemory limit:256mb description

Small hi usually a big hobby is playing the piano. We know that a musical melody is represented as a series of numbers consisting of N in length. Little hi has practiced a lot of music and found that many of the works themselves contain the same melody.

Melody can be expressed as a continuous sequence of numbers, similar melodies in the original sequence is not overlapping, for example, 1 2 3 2 3 2 1 in 2 3 2 appeared once, 2 3 appeared two times, little hi want to know a melody in the frequency of at least two times what is the longest melody?

Tips on how to solve problems

Input

The first line is an integer N. 1≤n≤100000

Next there are N integers that represent the number of each tone. 1≤ Digital ≤1000

Output

An integer line that represents the answer.

Sample Input
81 2 3 2 3 2 3 1
Sample Output
2
Analysis:

And POJ1743 is the same ... It's just a matter of two-point judgment. >= and > ...

POJ1743

Code:
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio>//by neighthornusing namespace Std;const int Maxn=100000+5;int N,S[MAXN],GS[MAXN],SA[MAXN],WV[MAXN],WB[MAXN],RAN[MAXN], height[maxn];inline BOOL cmp (int *x,int a,int b,int l) {return x[a]==x[b]&&x[a+l]==x[b+l];} inline void da (int *sa,int *x,int n,int m) {int i,j,p,*y=wb;for (i=0;i<m;i++) gs[i]=0;for (i=0;i<n;i++) gs[x[i]]++; for (i=1;i<m;i++) gs[i]+=gs[i-1];for (i=n-1;~i;i--) sa[--gs[x[i]]]=i;for (j=1,p=1;p<n;j<<=1,m=p) {for (i= n-j,p=0;i<n;i++) y[p++]=i;for (i=0;i<n;i++) if (sa[i]>=j) y[p++]=sa[i]-j;for (i=0;i<n;i++) wv[i]=x[y[i]]; for (i=0;i<m;i++) gs[i]=0;for (i=0;i<n;i++) gs[wv[i]]++;for (i=1;i<m;i++) gs[i]+=gs[i-1];for (i=n-1;~i;i--) Sa[--gs[wv[i]]]=y[i];p =1;swap (x, y), x[sa[0]]=0;for (i=1;i<n;i++) x[sa[i]]=cmp (y,sa[i],sa[i-1],j) p-1:p++;}} inline void calheight (int n) {int i,j,k=0;for (i=0;i<=n;i++) ran[sa[i]]=i;for (i=0;i<n;height[ran[i++]]=k) for (k? K--:233,j=sa[ran[i]-1];s[i+k]==s[j+k];k++);} inline bool Check (int x) {int min=n,max=0;bool ans=0;for (int i=1;i<=n;i++) {if (height[i]<x) {if (max!=0) if (max-min &GT;=X) Ans=1; Min=max=sa[i];continue;} Min=min (Min,sa[i]), Max=max (Max,sa[i]);} if (max!=0&&max-min>=x) Ans=1;return ans;} Signed main (void) {scanf ("%d", &n), for (int i=0;i<n;i++) scanf ("%d", &s[i]), Ran[i]=s[i];d a (sa,ran,n+1,1001 ); Calheight (n); int L=1,r=n>>1,ans=0;while (l<=r) {int mid= (l+r) >>1;if (check (mid)) ans=mid,l=mid+1; Elser=mid-1;} printf ("%d\n", ans); return 0;} The Cap ou pas cap. Pas Cap.

  

by Neighthorn

Hihocoder #1407: suffix array two • Repeat Melody 2

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.