hdu_6194 suffix Array +rmq

Source: Internet
Author: User

Well, desperate. Thought for five hours, finally still not a ... After the game looked at the suffix array instantly have the idea ... But because of too much food, thought for nearly two hours only then this problem kills.

First, it should be assumed that the suffix array is defined as all suffixes of a string s are arranged in a small order (using the subscript to denote the suffix). Because the process is not very understanding, but see Rujia Blue Book "Algorithm competition Dark Holy Code" can get a clever nlogn artifact algorithm. But that's not very important.

It can then be processed by the rank,height array correlation algorithm He mentioned in the LCP problem, and then the other can be thrown away.

The dark code defines the height array, height[k] meaning, the length of the longest common prefix between the Rank[i] array and the rank[i]-1 ... Based on this we can see some rules.

First, the definition of the height array has transitivity, it is easy to think of is ... Appearing and appearing only m times can be simply defined as, pass and can only be passed m times ...

So... We have the following algorithm.

#include <iostream>#include<stdio.h>#include<string.h>using namespacestd;Const Long Longmaxn=400233;Const Long Longinf=1e8+7;CharS[MAXN];//long Long DP[MAXN];Long LongSa[maxn],t[maxn],t2[maxn],c[maxn],n,len;Long LongR1ANK[MAXN],HEIGHT[MAXN];Long Longd[maxn][ -];voidRmq_init () { for(intI=0; i<n;++i) d[i][0]=Height[i];  for(intj=1;(1&LT;&LT;J) <=n;j++)         for(intI=0; i+ (1&LT;&LT;J)-1<n;i++) D[i][j]=min (d[i][j-1],d[i+ (1<< (J-1))][j-1]);}Long LongRMQ (intAintb) {    intk=0;  while((1<< (k +1)) <=b-a+1) k++; returnMin (d[a][k],d[b-(1&LT;&LT;K) +1][k]);}intm;voidBuild_sa (intm) {    Long Longi,*x=t,*y=T2;  for(i=0; i<m;++i) c[i]=0;  for(i=0; i<n;++i) c[x[i]=s[i]]++;  for(i=1; i<m;++i) c[i]+=c[i-1];  for(i=n-1; i>=0; i--) sa[--c[x[i]]]=i;  for(intk=1; k<= N; k*=2)    {        intp=0;  for(i=n-k;i<n;++i) y[p++]=i;  for(i=0; i<n;++i)if(sa[i]>=k) y[p++]=sa[i]-K;  for(i=0; i<m;++i) c[i]=0;  for(i=0; i<n;++i) c[x[y[i]]]++;  for(i=0; i<m;++i) c[i]+=c[i-1];  for(i=n-1; i>=0; i--) sa[--c[x[y[i]]]]=Y[i];        Swap (x, y); P=1; x[sa[0]]=0;  for(i=1; i<n;++i) {X[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p + +; }if(p>=n) Break; M=p; }}voidgetheight () {inti,j,k=0;  for(intI=0; i<n;++i) r1ank[sa[i]]=i;  for(intI=0; i<n;++i) {if(k) k--; intj=sa[r1ank[i]-1];  while(S[j+k]==s[i+k]) k++; Height[r1ank[i]]=K; } Height[n]=0;}intMain () {intT;cin>>T;  while(t--)    {//memset (height,0,sizeof (height));scanf"%d%s",&m,s); N=strlen (s); Len=n;n+=1;; Build_sa ( -);        GetHeight (); Rmq_init ();Long Longans=0;  for(intI=0; i<n;++i) {if(m==1) {ans+=len-sa[i]-max (height[i],height[i+1]); Continue; }            inta=i+1;intb=i+m-1; Long Longlimit=RMQ (A, b); if(N>b&&limit>max (height[i],height[b+1])) Ans+=limit-max (height[i],height[b+1]); } cout<<ans<<"\ n"; }    }

hdu_6194 suffix Array +rmq

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.