hdu5769--substring (suffix array)

Source: Internet
Author: User
Tags cas

Test instructions: The number of different substrings of a string containing a certain letter

A suffix array, which records the distance of the letters that need to appear at each location. Because I do not understand the suffix template card for a while, it is very simple.

Remember that both the SA and the height arrays are subscript 1-n.

//suffix Array#include <stdio.h>#include<cstring>#include<iostream>#include<algorithm>using namespaceStd;typedefLong Longll;Const intN =int(2E5) +Ten;intcmpint*r,intAintBintl) {    return(R[a]==r[b]) && (r[a+l]==r[b+l]);}//used to compare the first keyword with the second keyword,//The special place is, when preprocessing, r[n]=0 (less than the preceding characters)intWa[n],wb[n],wss[n],wv[n];intSa[n];//who's 1~n the first ?intRk[n],//who's in the first row?Height[n];//The longest common prefix length of the next two suffixes: suffix (sa[i-1]) and (Sa[i]).CharStr[n];intP[n];voidDA (Char*r,int*sa,intNintm) {//here n is more than 1 of the input n, a manually added character used to avoid CMP time out of bounds    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]=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 + +; }}voidCalheight (Char*r,int*sa,intN) {//here n is the actual length    inti,j,k=0;  for(i=1; i<=n;i++) rk[sa[i]]=i;  for(i=0;i<n; height[rk[i++]] =k) for(k?k--:0, j=sa[rk[i]-1]; R[I+K]==R[J+K]; k++);}intMainintargcChar Const*argv[]) {    //freopen ("in", "R", stdin);    intT; CIN>>T; intCAS =0;  while(t--) {printf ("Case #%d:", ++CAs); Chartmp[Ten]; Charx; scanf ("%s%s", TMP, str); X= *tmp;        GetChar (); intpos =-1; intn =strlen (str);  for(inti = n1; I >=0; --i) {if(Str[i] = = x) pos =i; P[i]=POS; } DA (str, SA, n+1, $);        Calheight (str, SA, n); ll ans=0; if(p[sa[1] ] != -1) ans + = n-p[sa[1] ];  for(inti =2; I <= N; ++i) {intnow =Sa[i]; intL = max (now+Height[i], P[now]); if(P[now] = =-1)Continue; Ans+ = n-l; } cout<< ans <<Endl; }    return 0;}

hdu5769--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.