Suffix array hihocoder repeat melody 1-4

Source: Internet
Author: User
Tags cmath

Konjac Konjac know today only to play suffix array, and still nlogn^2 ... But it's basically a run-through;

Repeat Melody 1:

Two-point answer, the height of the collection, Height<mid on the re-division, so as to ensure that each set of Lcp>=mid, routine board questions

Made by qt666#include<cstdio> #include <algorithm> #include <cmath> #include <iostream>#  Include<cstring>using namespace Std;typedef long long ll;const int N=1000050;int gi () {int x=0,flag=1;  Char Ch=getchar (); while (ch< ' 0 ' | |  Ch> ' 9 ') {if (ch== '-') Flag=-1;ch=getchar ();}  while (ch>= ' 0 ' &&ch<= ' 9 ') x=x*10+ch-' 0 ', Ch=getchar (); return X*flag;} int sa[n],n,len,y[n],rk,rnk[n],height[n],ans,a[n],k;struct data{int fir,sec,id;}  X[n];bool CMP (const data &AMP;A,CONST data &b) {if (A.fir==b.fir) return a.sec<b.sec; else return A.fir<b.fir;}  void Work2 () {rk=1;y[x[1].id]=rk; for (int i=2;i<=len;i++) {if (x[i-1].fir!=x[i].fir| |  X[I-1].SEC!=X[I].SEC) rk++;  Y[x[i].id]=rk;  }}void work () {sort (x+1,x+1+len,cmp); Work2 (); for (int i=1;i<=len;i<<=1) {for (int j=1;j+i<=len;j++) x[j].fir=y[j],x[j].sec=y[j+i],x[j].id=j;for (int j=  len-i+1;j<=len;j++) X[j].fir=y[j],x[j].sec=0,x[j].id=j;sort (x+1,x+1+len,cmp); Work2 (); if (Rk==len) break; }}vOID Get_height () {int kk=0;for (int i=1;i<=len;i++) rnk[sa[i]]=i;for (int i=1;i<=len;i++) {if (KK) Kk--;int j=sa[rnk [I]-1];while (A[i+kk]==a[j+kk]) Kk++;height[rnk[i]]=kk;}} BOOL Check (int mid) {int ret=1,size=1;for (int i=2;i<=len;i++) {if (Height[i]<mid) Ret=max (ret,size), Size=1;else size++;} return ret>=k;}  int main () {Len=gi (); K=gi (); for (int i=1;i<=len;i++) A[i]=gi ();  for (int i=1;i<=len;i++) x[i].id=i,x[i].fir=x[i].sec=a[i];  Work (); for (int i=1;i<=len;i++) sa[y[i]]=i;  Get_height (); int l=0,r=len;  while (l<=r) {int mid= (L+R) >>1;  if (check (mid)) ans=mid,l=mid+1;  else r=mid-1;  } printf ("%d\n", ans); return 0;}

Repeat Melody 2:

Similar to the above question, the two answers, the height of the set, maintain the most left and right side of the set, to judge the end of the subtraction >=mid;

Made by qt666#include<cstdio> #include <algorithm> #include <cmath> #include <iostream>#  Include<cstring>using namespace Std;typedef long long ll;const int N=1000050;int gi () {int x=0,flag=1;  Char Ch=getchar (); while (ch< ' 0 ' | |  Ch> ' 9 ') {if (ch== '-') Flag=-1;ch=getchar ();}  while (ch>= ' 0 ' &&ch<= ' 9 ') x=x*10+ch-' 0 ', Ch=getchar (); return X*flag;} int sa[n],n,len,y[n],rk,rnk[n],height[n],ans,a[n],k;struct data{int fir,sec,id;}  X[n];bool CMP (const data &AMP;A,CONST data &b) {if (A.fir==b.fir) return a.sec<b.sec; else return A.fir<b.fir;}  void Work2 () {rk=1;y[x[1].id]=rk; for (int i=2;i<=len;i++) {if (x[i-1].fir!=x[i].fir| |  X[I-1].SEC!=X[I].SEC) rk++;  Y[x[i].id]=rk;  }}void work () {sort (x+1,x+1+len,cmp); Work2 (); for (int i=1;i<=len;i<<=1) {for (int j=1;j+i<=len;j++) x[j].fir=y[j],x[j].sec=y[j+i],x[j].id=j;for (int j=  len-i+1;j<=len;j++) X[j].fir=y[j],x[j].sec=0,x[j].id=j;sort (x+1,x+1+len,cmp); Work2 (); if (Rk==len) break; }}vOID Get_height () {int kk=0;for (int i=1;i<=len;i++) rnk[sa[i]]=i;for (int i=1;i<=len;i++) {if (KK) Kk--;int j=sa[rnk [I]-1];while (A[i+kk]==a[j+kk]) Kk++;height[rnk[i]]=kk;}} BOOL Check (int mid) {int minn=sa[1],maxn=sa[1];for (int i=2;i<=len;i++) {if (Height[i]<mid) {if (maxn-minn>=mid ) return 1;minn=maxn=sa[i];} Minn=min (Minn,sa[i]), Maxn=max (Maxn,sa[i]);} return 0;}  int main () {Len=gi (); for (int i=1;i<=len;i++) A[i]=gi ();  for (int i=1;i<=len;i++) x[i].id=i,x[i].fir=x[i].sec=a[i];  Work (); for (int i=1;i<=len;i++) sa[y[i]]=i;  Get_height (); int l=0,r=len;  while (l<=r) {int mid= (L+R) >>1;  if (check (mid)) ans=mid,l=mid+1;  else r=mid-1;  } printf ("%d\n", ans); return 0;}

Repeat Melody 3:

Or the board, the two strings with "#" separate, and then the two-part answer to divide the height collection

Made by qt666#include<cstdio> #include <algorithm> #include <cmath> #include <iostream>#  Include<cstring>using namespace Std;typedef long long ll;const int N=1000050;int gi () {int x=0,flag=1;  Char Ch=getchar (); while (ch< ' 0 ' | |  Ch> ' 9 ') {if (ch== '-') Flag=-1;ch=getchar ();}  while (ch>= ' 0 ' &&ch<= ' 9 ') x=x*10+ch-' 0 ', Ch=getchar (); return X*flag;} int Sa[n],n,len,y[n],rk,rnk[n],height[n],ans,cor[n],num[3];char ch[n],ch2[n],a[n];struct data{int fir,sec,id;}  X[n];bool CMP (const data &AMP;A,CONST data &b) {if (A.fir==b.fir) return a.sec<b.sec; else return A.fir<b.fir;}  void Work2 () {rk=1;y[x[1].id]=rk; for (int i=2;i<=len;i++) {if (x[i-1].fir!=x[i].fir| |  X[I-1].SEC!=X[I].SEC) rk++;  Y[x[i].id]=rk;  }}void work () {sort (x+1,x+1+len,cmp); Work2 (); for (int i=1;i<=len;i<<=1) {for (int j=1;j+i<=len;j++) x[j].fir=y[j],x[j].sec=y[j+i],x[j].id=j;for (int j= len-i+1;j<=len;j++) X[j].fir=y[j],x[j].sec=0,x[j].id=j;sort (x+1,x+1+len,cmp); wORK2 (); if (Rk==len) break; }}void get_height () {int kk=0;for (int i=1;i<=len;i++) rnk[sa[i]]=i;for (int i=1;i<=len;i++) {if (KK) Kk--;int j=sa[ Rnk[i]-1];while (A[i+kk]==a[j+kk]) Kk++;height[rnk[i]]=kk;} BOOL Check (int mid) {memset (num,0,sizeof (num)); num[cor[sa[1]]]++;for (int i=2;i<=len;i++) {if (Height[i]<mid) { if (num[1]&&num[2]) return 1;memset (num,0,sizeof (num));} num[cor[sa[i]]]++;} return 0;}  int main () {scanf ("%s", ch+1), scanf ("%s", ch2+1);  int Len1=strlen (ch+1), Len2=strlen (ch2+1); len=len1+len2+1;  for (int i=1;i<=len1;i++) a[i]=ch[i],cor[i]=1;a[len1+1]= ' # ';  for (int i=1;i<=len2;i++) a[len1+1+i]=ch2[i],cor[len1+1+i]=2;  for (int i=1;i<=len;i++) x[i].id=i,x[i].fir=x[i].sec=a[i]-' a ' +1;  Work (); for (int i=1;i<=len;i++) sa[y[i]]=i;  Get_height ();  int l=0,r=min (LEN1,LEN2);  while (l<=r) {int mid= (L+R) >>1;  if (check (mid)) ans=mid,l=mid+1;  else r=mid-1;  } printf ("%d\n", ans); return 0;}

  

Repeat Melody 4:

The real fire problem ...

First the violence needs to enumerate the length L, and the string start position I, then start with I, the answer of the loop length is 1+LCP (i,i+l)/L, which is recorded as K (i,l); This picture is still well understood.

Optimization can only consider the position of the integer multiples of L, and then for the x that is not the integer, the nearest L after him is the integer multiples of p, then K (x,l) is not greater than K (p,l) +1,

Because we know I and i+l LCP, then I+LCP and I+L+LCP is mismatch, otherwise LCP can also longer, so here is GI.

Then because the X and P difference is not more than L, so the value of k more than 1;

Then assume that there is an X that makes K (x,l) ==k (p,l) +1, then x=i-l+lcp%l, which quite moves the mismatch position forward (cont.) An L, you can draw an understanding, so you only need to Judge K (x,l)

And then

for (int l=1; l<=len; l++) {

for (int i=l;i+l<=len;i+=l) {

}

}

is a classic Nlogn complexity, very good ... LCP is the height of the RMQ minimum value can be, ST table can be. Wonderful

Made by qt666#include<cstdio> #include <algorithm> #include <cmath> #include <iostream>#  Include<cstring>using namespace Std;typedef long long ll;const int N=200050;int gi () {int x=0,flag=1;  Char Ch=getchar (); while (ch< ' 0 ' | |  Ch> ' 9 ') {if (ch== '-') Flag=-1;ch=getchar ();}  while (ch>= ' 0 ' &&ch<= ' 9 ') x=x*10+ch-' 0 ', Ch=getchar (); return X*flag;} int Sa[n],len,y[n],rk,rnk[n],height[n],ans,pre[n],pre2[n],st[n][20];char ch[n],ch2[n],a[n];struct data{int fir,sec , id;}  X[n];bool CMP (const data &AMP;A,CONST data &b) {if (A.fir==b.fir) return a.sec<b.sec; else return A.fir<b.fir;}  void Work2 () {rk=1;y[x[1].id]=rk; for (int i=2;i<=len;i++) {if (x[i-1].fir!=x[i].fir| |    X[I-1].SEC!=X[I].SEC) rk++;  Y[x[i].id]=rk;  }}void work () {sort (x+1,x+1+len,cmp); Work2 ();    for (int i=1;i<=len;i<<=1) {for (int j=1;j+i<=len;j++) x[j].fir=y[j],x[j].sec=y[j+i],x[j].id=j;    for (int j=len-i+1;j<=len;j++) x[j].fir=y[j],x[j].sec=0,x[j].id=j; SORT (X+1,X+1+LEN,CMP); Work2 ();  if (Rk==len) break;  }}void get_height () {int kk=0;for (int i=1;i<=len;i++) rnk[sa[i]]=i;    for (int i=1;i<=len;i++) {if (KK) kk--;    int j=sa[rnk[i]-1];    while (A[i+kk]==a[j+kk]) kk++;  HEIGHT[RNK[I]]=KK;  }}void Make_st () {pre[0]=1;for (int i=1;i<=16;i++) pre[i]=pre[i-1]<<1;  pre2[0]=-1;for (int i=1;i<=len;i++) pre2[i]=pre2[i>>1]+1;  for (int i=2;i<=len;i++) st[i][0]=height[i]; for (int j=1;j<=16;j++) for (int i=2;i<=len;i++) {if (I+pre[j]-1<=len) {st[i][j]=min (st[i][j-1],st[i+pre[j-1]      ][j-1]);  }}}int Query (int l,int r) {int x=pre2[r-l+1]; return min (st[l][x],st[r-pre[x]+1][x]);}  int LCP (int l,int r) {if (l>r) swap (L,R); return query (L+1,R);}  int main () {scanf ("%s", a+1); Len=strlen (a+1);  for (int i=1;i<=len;i++) x[i].id=i,x[i].fir=x[i].sec=a[i]-' a ' +1;  Work (); for (int i=1;i<=len;i++) sa[y[i]]=i;  Get_height (); Make_st (); for (int l=1; l<=len; l++) {for (int i=l;i+l<=len;i+=l) {int LCP=LCP (Rnk[i],rnk[i+l]); Ans=max (ANS,1+LCP/L);    Ans=max (ANS,LCP (rnk[i-l+lcp%l],rnk[i+lcp%l])/l+1);  }} printf ("%d\n", ans); return 0;}

  

Suffix array hihocoder repeat melody 1-4

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.