HDU 5769 Substring (suffix array)

Source: Internet
Author: User

Substring

Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 1570 Accepted Submission (s): 618


Problem Description?? Is practicing his program skill, and now he's given a string, he has to calculate the total number of its distinct substr Ings.
But?? Thinks that's too easy, he wants to make this problem more interesting.
?? Likes a character x very much, so he wants to know the number of distinct substrings which contains at least one X.
However,?? is unable to solve it, please help him.

Inputthe first line of the input gives the number of test cases T; T test Cases follow.
Each test case is consist of 2 lines:
First line was a character X, and second line is a string S.
X is a lowercase letter, and S contains lowercase letters (' a '-' Z ') only.

T<=30
1<=| S|<=10^5
The Sum of | s| In all the test cases are no more than 700,000.

Outputfor each test case, output one line containing ' case #x: Y ' (without quotes), where x is the ' test Case number (Startin G from 1) and Y-is the answer.

Sample INPUT2 a ABC b BBB

Sample outputcase #1:3 case #2:3 HintIn first case, all distinct substrings containing at least one a:a, AB, ABC. In second case, all distinct substrings containing at least one b:b, BB, BBB.

Authorfzu Analysis: The topic of a particular letter of the non-repeating substring, we know that the non-repeating substring is the basic use of the suffix array, you can first find the total non-repeating substring. Then the non-repeating substring containing the letter is not included, and the last two are subtracted, that is, when the non-repeating substring that does not contain the letter is evaluated, You can first find the total number of substrings that do not contain the letter. When you go to heavy, subtract only the height value that does not contain a specific letter. In this case, we need to record each letter with an array, the position of the nearest specific letter on the right. The code is as follows:
#include <cstdio>#include<iostream>#include<algorithm>#include<cstring>typedefLong Longll;using namespacestd;Const intmaxn=200010;intR[MAXN];intWA[MAXN],WB[MAXN],WV[MAXN],WS[MAXN];CharSTR[MAXN];intcmpint*r,intAintBintl) {returnr[a]==r[b]&&r[a+l]==r[b+l];}voidDaConst CharR[],intSa[],intNintM//N is len+1,m generally larger than the largest number in the array{      inti,j,p,*x=wa,*y=wb,*T;  for(i=0; i<m; i++) ws[i]=0;  for(i=0; i<n; i++) ws[x[i]=r[i]]++;  for(i=1; i<m; i++) ws[i]+=ws[i-1];  for(i=n-1; i>=0; i--) sa[--ws[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++) ws[i]=0;  for(i=0; i<n; i++) ws[wv[i]]++;  for(i=1; i<m; i++) ws[i]+=ws[i-1];  for(i=n-1; i>=0; i--) sa[--ws[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;}intSA[MAXN],RANK[MAXN],HEIGHT[MAXN];//The SA is found in the position of the string by the suffix rank, where rank is the suffix rank based on the position, the inverse of which is the minimum value of the SA array in the template is 1. voidCalheight (Const Char*r,int*sa,intN) {      inti,j,k=0;  for(i=1; i<=n; i++) rank[sa[i]]=i;  for(i=0; i<n; height[rank[i++]]=k) for(k?k--:0, j=sa[rank[i]-1]; R[I+K]==R[J+K]; k++);}intMain () {intT,len,case=0;; Charch; scanf ("%d",&t);  while(t--) { case++; ll Ans2=0; Memset (R,0,sizeof(R)); scanf ("%c",&ch); scanf ("%s", str); Len=strlen (str); intRr=Len;  for(inti=len-1; i>=0; i--)      {         if(str[i]!=ch) {R[i]=RR; }         Else{ll h= (rr-i-1); RR=i; R[i]=RR; Ans2+=h* (H +1)/2; }} ans2+=rr* (rr+1)/2; //cout<<ans2<<endl;Da (str,sa,len+1, the);       Calheight (Str,sa,len);  for(intI=2; i<=len;i++) {Ans2-=min (height[i],r[sa[i]]-Sa[i]); } ll ans=(Long Long) len* (len+1)/2;  for(intI=2; i<=len;i++) {ans-=Height[i]; }    //cout<<ans<< "" <<ans2<<endl;printf"Case #%d:%lld\n", case,ans-ans2); }return 0;}

HDU 5769 Substring (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.