Hdu 6194 String string string (suffix array)

Source: Internet
Author: User

Topic Link: Hdu 6194 string string

Test instructions

Give you a string, give you a K, ask you how many substrings happen to occur in the original string k times.

Exercises

After the suffix array is calculated, use the information of the height array to find the answer.

Each time an interval of k length is used to get the height array of the card, the LCP of the interval is obtained.

The contribution of this interval is Ans=lcp-max (Height[i],height[i+k]).

If ans<=0, it does not contribute.

Like 2 AAA.

The suffix array is:

1 A

2 AA

3 AAA

Height is 0,1,2

Now sweep to [1,2],lcp=1,max (Height[i],height[i+k]) =2,ans=-1, so this interval does not contribute

to [2,3],lcp=2,max (Height[i],height[i+k]) =1,ans=1, so the interval contributes 1.

1#include <bits/stdc++.h>2 #defineF (I,A,B) for (int i=a;i<=b;++i)3 using namespacestd;4 5 namespacesuffixarray{6     #defineFN (n) for (int i=0;i<n;i++)7     Const intN =1e5+7;8     intRnk[n],sa[n],height[n],c[n];CharS[n];9     voidGetsa (intNintMint*X=RNK,int*y=height) {TenFN (m) c[i]=0; FN (n) c[x[i]=s[i]]++; FN (m) c[i+1]+=C[i]; One          for(inti=n-1; i>=0; i--) sa[--c[x[i]]]=i; A          for(intk=1, p;p=0, K<=n;k=p>=n? n:k<<1, m=p) { -              for(inti=n-k;i<n;i++) y[p++]=i; -FN (N)if(sa[i]>=k) y[p++]=sa[i]-K; theFN (m) c[i]=0; FN (n) c[x[y[i]]]++; FN (m) c[i+1]+=C[i]; -              for(inti=n-1; i>=0; i--) sa[--c[x[y[i]]]]=Y[i]; -Swap (x, y), p=1, x[sa[0]]=0; -              for(intI=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 + +; -         } +FN (n) rnk[sa[i]]=i; A          for(intI=0, j,k=0; i<n-1; height[rnk[i++]]=k) at          for(k=k?k-1: k,j=sa[rnk[i]-1];s[i+k]==s[j+k];k++); -     } - } - using namespaceSuffixarray; -  - intt,k,n,f[n][ -]; in  - voidRmqint*a) to { +      for(intI=1; i<=n;i++) f[i][0]=A[i]; -      for(intj=1;1<<j<n;j++) for(intI=1; i<=n;i++) the     if(I+ (1&LT;&LT;J)-1<=n) F[i][j]=min (f[i][j-1],f[i+ (1<<j-1)][j-1]); *     Else  Break; $ }Panax NotoginsengInlineintFindintLintR) - { the     intk= to-__BUILTIN_CLZ (r-l+1); +     returnMin (f[l][k],f[r-(1&LT;&LT;K) +1][k]); A } the  + intMain () - { $scanf"%d",&t); $      while(t--) -     { -scanf"%d%s",&k,s); theN=strlen (s), Getsa (n+1, -); -height[n+1]=0; rmq (height);Wuyi         intans=0; theF (I,1, N) -         { Wu             if(i+k-1>n)Continue; -             intLCP; About             if(i+1>i+k-1) lcp=n-Sa[i]; $             ElseLcp=find (i+1, i+k-1); -Lcp-=max (height[i],height[i+K]); -             if(lcp<=0)Continue; -ans+=LCP; A         } +printf"%d\n", ans); the     } -     return 0; $}
View Code

Hdu 6194 String string string (suffix array)

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.