"Suffix array" "dichotomy answer" "differential" poj1743 musical Theme

Source: Internet
Author: User

The difference cancellation plus minus one is worth the influence, it seems that the upper bound of R two points to be set to (n-2)/2? Why?

SA cannot overlap the longest repeating substring

Given a string, the longest repeating substring, the two substrings, cannot overlap.
Algorithm Analysis:
This question is slightly more complicated than the previous one. The first two points, the question into a decision-making problem: judge whether
There are two substrings of length k that are the same and do not overlap. The key to solving this problem is to use
The height array. Divides the sorted suffix into groups, where the height value between the suffixes of each group is
Not less than K.

It is easy to see that there is a hope that the longest common prefix of two suffixes not less than k must be in the same group. Then
For each set of suffixes, only the difference between the maximum and minimum values of the SA value for each suffix is not less than
K. If there is a set of satisfies, the description exists, otherwise it does not exist. The time complexity of the whole procedure is
O (NLOGN).

#include <cstdio> #include <algorithm> #include <cstring>using namespace std; #define N 20001#define INF 2147483647int n,s[n],tong[n],sa[n],t[n],t2[n],rank[n],lcp[n];bool Check (int x) {int maxsa=sa[0],minsa=sa[0];for ( int i=1;i<=n;++i) {if (lcp[i]<x| |    I==n) {if (maxsa-minsa>=x) return 1;    Maxsa=minsa=sa[i];    } else if (lcp[i]>=x) {Maxsa=max (maxsa,sa[i]);    Minsa=min (Minsa,sa[i]); }}return 0;} BOOL CMP (int *y,int i,int k) {return ((Y[sa[i-1]]==y[sa[i]) && ((sa[i-1]+k>=n?-1:y[sa[i-1]+k]) = = (sa[i]+k &GT;=N?-1:Y[SA[I]+K]));} void Build_sa (int range) {int *x=t,*y=t2;memset (tong,0,sizeof (int) *range), for (int i=0;i<n;++i) tong[x[i]=s[i]]++; for (int i=1;i<range;++i) tong[i]+=tong[i-1];for (int i=n-1;i>=0;--i) sa[--tong[x[i]]]=i;for (int k=1;k<=n;k  <<=1) {int p=0;  for (int i=n-k;i<n;++i) y[p++]=i;  for (int i=0;i<n;++i) if (sa[i]>=k) y[p++]=sa[i]-k;  memset (tong,0,sizeof (int) *range); for (int i=0;i<n;++i) Tong[x[y[i]]++;  for (int i=1;i<range;++i) tong[i]+=tong[i-1];  for (int i=n-1;i>=0;--i) sa[--tong[x[y[i]]]]=y[i]; Swap (x, y); P=1;  x[sa[0]]=0;  for (int i=1;i<n;++i) x[sa[i]]=cmp (y,i,k)? p-1:p++;  if (p>=n) break;  Range=p;  }}void GET_LCP () {int k=0;for (int i=0;i<n;++i) rank[sa[i]]=i;for (int i=0;i<n;++i) if (Rank[i]) {if (k)--k;  int j=sa[rank[i]-1];  while (S[i+k]==s[j+k]) ++k;  Lcp[rank[i]]=k;  }}int T3[n];int Main () {while (1) {scanf ("%d", &n);  if (!n) break;  for (int i=0;i<n;++i) {scanf ("%d", &t3[i]);  s[i]=t3[i]-t3[i-1]+89;  } BUILD_SA (200);  GET_LCP ();  int l=0,r= (n-2>>1);    while (r>l) {int mid= (l+r+1>>1);    if (check (mid)) L=mid;    else r=mid-1;  } printf ("%d\n", l>=4?l+1:0); }return 0;}

"suffix array" "dichotomy answer" "differential" poj1743 musical Theme

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.