Given a character x and a string. Requires that all different strings containing this character be output.
The suffix array computes all the different strings of a string and understands the principle to do so.
For each suffix I, will produce a prefix of len-sa[i]-hight[i], and add up is all the different strings. This requires that the string must contain X
That is, for each suffix I, subtract the prefix that does not contain x.
Save the position of each x, query the first x in the suffix with lower_bound, and then do not calculate the contribution.
DC3 is too slow to knock.
1#include <cstdio>2#include <algorithm>3#include <cstring>4 #defineLL Long Long5 //using namespace std;6 7 Const intMAXN = 1e5+ -;8 #defineF (x) ((x)/3+ ((x)%3 = = 1?0:TB))9 #defineG (x) ((x) <TB? ( x) *3+1: (((x)-TB) *3+2)Ten intwa[maxn*3],wb[maxn*3],wv[maxn*3],wss[maxn*3]; One A intC0 (int*r,intAintb) - { - returnR[a] = = R[b] && r[a+1] = = r[b+1] && r[a+2] = = r[b+2]; the } - intC12 (intKint*r,intAintb) - { - if(k = =2) + returnR[a] < R[b] | | (R[a] = = R[b] && C12 (1, r,a+1, B +1)); - Else + returnR[a] < R[b] | | (R[a] = = R[b] && wv[a+1] < wv[b+1]); A } at voidSortint*r,int*a,int*b,intNintm) - { - inti; - for(i=0; i<n;i++) Wv[i] =R[a[i]]; - for(i=0; i<m;i++) Wss[i] =0; - for(i=0; i<n;i++) wss[wv[i]]++; in for(i=1; i<m;i++) Wss[i] + = wss[i-1]; - for(i=n-1; i>=0; i--) B[--wss[wv[i]] =A[i]; to } + voidDC3 (int*r,int*_sa,intNintm) - { the intI,j,*rn = r+N; * int*san = _sa+n,ta =0, tb= (n+1)/3, tbc=0, p; $R[n] = r[n+1] =0;Panax Notoginseng for(i=0; i<n;i++)if(i%3!=0) wa[tbc++] =i; -Sort (r+2, wa,wb,tbc,m); theSort (r+1, wb,wa,tbc,m); + sort (r,wa,wb,tbc,m); A for(p=1, Rn[f (wb[0])] =0, i=1; i<tbc;i++) theRn[f (Wb[i])] = C0 (r,wb[i-1],wb[i])? P1:p + +; + if(P <tbc) DC3 (rn,san,tbc,p); - Else for(i=0; i<tbc;i++) San[rn[i]] =i; $ for(i=0; i<tbc;i++)if(SAN[I]<TB) wb[ta++] = san[i]*3; $ if(n%3==1) wb[ta++] = n1; - sort (r,wb,wa,ta,m); - for(i=0; i<tbc;i++) wv[wb[i]=g (san[i]) =i; the for(i=0, j=0, p=0; i<ta&&j<tbc;p++) -_SA[P] = C12 (wb[j]%3, R,wa[i],wb[j])? wa[i++]:wb[j++];Wuyi for(; i<ta;p++) _sa[p] = wa[i++]; the for(; j<tbc;p++) _sa[p] = wb[j++]; - } Wu voidDaintStr[],int_sa[],int_rank[],int_height[],intNintm) - { About for(inti=n;i<n*3; i++) Str[i] =0; $DC3 (str,_sa,n+1, m); - intI,j,k =0; - for(i=1; i<=n;i++) _rank[_sa[i]] =i; - for(i=0; i<n;i++) A { + if(k) k--; thej = _sa[_rank[i]-1 ]; - while(Str[i+k] = = Str[j+k]) k++; $_height[_rank[i]] =K; the } the } the the intT; - intsa[3*maxn],hehe[3*MAXN],RANK[MAXN],HEIGHT[MAXN]; in intx; the Charline[3*MAXN]; the About voidDebugintN) the { the for(intI=0; i<=n;i++) the { +printf"sa:%d rank:%d height:%d\n", Sa[i],rank[i],height[i]); - } the }Bayi the intMain () the { - //freopen ("Input.txt", "R", stdin); -scanf"%d",&T); the intAa1 =0; the while(t--) the { thescanf"%c%s",&x,line); - intLen =strlen (line); the for(intI=0; i<len;i++) Hehe[i] =Line[i]; the intch[maxn],cnt =0; the for(intI=0; i<len;i++)94 { the if(Line[i] = = x) ch[cnt++] =i; the } thech[cnt++] =Len;98Da (Hehe,sa,rank,height,len, -); About //debug (len); -LL ans =0;101 for(intI=1; i<=len;i++)102 {103 intpos = Ch[std::lower_bound (ch,ch+cnt,sa[i])-ch]-Sa[i];104Ans + = len-Sa[i]; theAns-=Std::max (Pos,height[i]);106 }107printf"Case #%d:%i64d\n",++Cas,ans);108 }109}
hdu5769-substring-Multi-school #4-1006-suffix Array