Hdu 6194 Shenyang Online race--string string string (suffix array)

Source: Internet
Author: User

Topic links

problem DescriptionUncle Mao is a wonderful acmer. One day he met a easy problem, but Uncle Mao is so lazy this he left the problem. I hope you can give him a solution.
Given A string s, we define a substring that happens exactly k Times as an important string, and you need to the find out how many substrings which is important strings.

InputThe first line contains an integerT(T≤ ) implying the number of test cases.
For each test case, there is lines:
The first line contains an integerk(k≥1 ) which is described above;
The second line contain a strings(lengtH(s)≤5 ).
It ' s guaranteed that∑ l en gt h ( S) ≤ 2∗ 10 6 .

OutputFor each test case, print the number of the important substrings in a line.

Sample Input22abcabc3abcabcabcabc

Sample Output69 Test Instructions: There is a string s, how many of the substrings that happen to have exactly k times? Idea: suffix array, through the suffix array algorithm can know the rank of each suffix, if there is a substring happens to happen K, then there must be k corresponding suffix that this substring is the prefix of this k suffix string, then this k suffix string ranking must be continuous, so we ranked from 1~len (s) sequentially Take a continuous k suffix string, can be based on the height[] array to quickly calculate the current K suffix string of the maximum common prefix length len, then a length of 1 to Len prefix substring, this k string contains, set the current start K strings for I to i+k-1, then if the substring length is too short, may i-1 or i+k This string also contains the corresponding substring, so calculate I and i-1 string, i+k and i+k-1 the maximum common prefix is m, then the substring length must be greater than m to ensure that i-1 and i+k does not contain the corresponding substring, only i~i+k-1 this k string contains the corresponding substring. The code is as follows:
#include <iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<cmath>using namespaceStd;typedefLong LongLL;Const intn=1e5+5;CharS[n];intK;intWa[n],wb[n],wv[n],wss[n];intSa[n],ran[n],height[n];intf[n][ -];intcmpint*r,intAintBintl) {    returnr[a]==r[b]&&r[a+l]==r[b+l];}voidDaChar*r,int*sa,intNintm) {    inti,j,p,*x=wa,*y=wb,*T;  for(i=0; i<m; i++) wss[i]=0;  for(i=0; i<n; i++) wss[x[i]= (int) r[i]]++;  for(i=1; i<m; i++) wss[i]+=wss[i-1];  for(i=n-1; i>=0; i--) sa[--wss[x[i]]]=i;  for(j=1, p=1; p<n; j*=2, m=p) { for(p=0, I=n-j; i<n; i++) y[p++]=i;  for(i=0; i<n; i++)if(SA[I]&GT;=J) y[p++]=sa[i]-J;  for(i=0; i<n; i++) wv[i]=X[y[i]];  for(i=0; i<m; i++) wss[i]=0;  for(i=0; i<n; i++) wss[wv[i]]++;  for(i=1; i<m; i++) wss[i]+=wss[i-1];  for(i=n-1; i>=0; i--) sa[--wss[wv[i]]]=Y[i];  for(t=x,x=y,y=t,p=1, x[sa[0]]=0, i=1; i<n; i++) X[sa[i]]=CMP (y,sa[i-1],sa[i],j)? p1:p + +; }    return;}voidCallheight (Char*r,int*sa,intN) {    inti,j,k=0;  for(i=1; i<=n;i++) Ran[sa[i]]=i;  for(i=0; i<n;height[ran[i++]]=k) for(k?k--:0, j=sa[ran[i]-1];r[i+k]==r[j+k];k++); return ;}voidInitintLen) {     for(intI=1; i<=len;i++) f[i][0]=Height[i];  for(ints=1;(1<<s) <=len;s++)    {        intTmp= (1<<s);  for(intI=1; i+tmp-1<=len;i++) {F[i][s]=min (f[i][s-1],f[i+tmp/2][s-1]); }    }}intCalintLintR) {    intLEN=LOG2 (r-l+1); intAns=min (f[l][len],f[r-(1<<len) +1][len]); returnans;}intMain () {intT Cin>>u;  while(t--) {scanf ("%d%s",&k,s); intlen=strlen (s); Da (S,sa,len+1, the);       Callheight (S,sa,len);       Init (len); intans=0;  for(intI=1; i+k-1<=len;i++)       {           intj=i+k-1; inttmp=Height[i]; if(j+1<=len) Tmp=max (tmp,height[j+1]); intx; if(k!=1) {x=cal (i+1, J); }           Elsex=len-Sa[i]; Ans+=max (0, X-tmp); } printf ("%d\n", ans); }    return 0;}

Hdu 6194 Shenyang Online race--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.