a180285 was fortunate to have been chosen as an international student for the Earth to meow. He found that it was interesting to see the cat names before class. Suppose there are n cats in the class, and each meow name consists of a surname and a name. The teacher on the Meow Planet chooses M-string to roll the name, and each time a string is read out, if the string is a substring of the surname or name of a Mew, then the MEW must answer. However, because the loadline of the Mew are too eccentric to be expressed in ASCII. To facilitate the description, a180285 decided to use several strings to denote the name of the Mew. now can you help a180285 count the number of cats answered at each call, and how many times do each meow answer after the end of the M-roll?
it should be considered as a simple application of the suffix array ...
First, connect all the strings, separating them with different separators.
The suffix array is then sa,rank,height the array.
finally enumerate each name string, starting with the rank[name string beginning] to expand to the left and right
the expansion here refers to the expansion of the SA subscript .
Edge Statistics accumulate the number of points to each meow .
In order to avoid a single call in the Count of a cat many times, as well as a cat named at a time and counted several times
(actually the two are the same ...) )
can be handled with a single label array.
however this label array if every time Fillchar is obviously unrealistic ... Because it's too slow ...
then we apply the idea of the top label update in the KM algorithm.
Of course, it's also possible to do it again and restore the array.
In addition, a PE on the BZ ... Note that there is no space at the end of the line
1 Program bzoj2754;2 ConstINF =100007; MAXN =400010;3 varTota,totb,i,cnt,n,x,j:longint;4a,rank,s,tmp,sa,height,pos,ter,len,vis,ans:array[-1.. Maxn]of Longint;5 6 function Max (a,b:longint): Longint;7 begin8 ifA>b then exit (a)Elseexit (b);9 end;Ten One procedure Suffix_array; A varSz,i,j,p,v0,v1,v00,v01:longint; - begin -Sz:=max (n,inf+10000); the fori:=0To SZ Dos[i]:=0; - fori:=0to n1 Dorank[i]:=A[i]; - fori:=0to n1 DoInc (S[rank[i]); - fori:=1To SZ DoInc (s[i],s[i-1]); + fori:=n-1Downto0 Do - begin + Dec (s[rank[i]]); Asa[s[rank[i]]]:=i; at end; -j:=1; - whileJ<=n Do - begin -p:=0; - forI:=n-j to N1 Do in begin -tmp[p]:=I;inc (p); to end; + fori:=0to n1 Do ifsa[i]-j>=0 Then - begin thetmp[p]:=sa[i]-J;inc (p); * end; $ fori:=0To SZ Dos[i]:=0;Panax Notoginseng fori:=0to n1 DoInc (S[rank[i]); - fori:=1To SZ DoInc (s[i],s[i-1]); the fori:=n-1Downto0 Do + begin A Dec (s[rank[tmp[i]]); thesa[s[rank[tmp[i]]]]:=Tmp[i]; + end; -p:=0; tmp[sa[0]]:=0; $ fori:=1to n1 Do $ begin -v0:=sa[i-1];v1:=Sa[i]; - ifV0+j<n then V00:=rank[v0+j]Elsev00:=-1; the ifV1+j<n then V01:=rank[v1+j]Elsev01:=-1; - if(Rank[v0]=rank[v1]) and (V00=V01) then tmp[sa[i]]:=pElseWuyi begin theInc (P); tmp[sa[i]]:=p; - end; Wu end; - fori:=0to n1 Dorank[i]:=Tmp[i]; AboutJ:=j <<1; $ end; - end; - - function Compare (i,j,x:longint): Longint; A begin + while(i+x-1<n) and (j+x-1<n) and (a[i+x-1]=a[j+x-1]) DoInc (X); theExit (x1); - end; $ the procedure Calc_height; the varI:longint; the begin the ifrank[0]=0Then height[0]:=0 Elseheight[0]:=compare (0, sa[rank[0]-1],1); - fori:=1to n1 Do in ifrank[i]=0Then height[i]:=0 ElseHeight[i]:=compare (i,sa[rank[i]-1],max (height[i-1],1)); the end; the About function Solve (x,y:longint): Longint; the varI,tot:longint; the begin thex:=rank[x];i:=x;tot:=0; + while(i>=1) and (Height[sa[i]]>=y) Do - begin the if(pos[sa[i-1]]<>0) and (vis[pos[sa[i-1]]]<>x) ThenBayi begin theInc (TOT); vis[pos[sa[i-1]]]:=x;inc (ans[pos[sa[i-1]]]); the end; - Dec (i); - end; thei:=x+1; the while(I<n) and (height[sa[i]]>=y) Do the begin the if(pos[sa[i]]<>0) and (vis[pos[sa[i]]]<>x) Then - begin theInc (TOT); vis[pos[sa[i]]]:=X;inc (Ans[pos[sa[i]]); the end; the Inc (i);94 end; the exit (TOT); the end; the 98 begin AboutREADLN (TOTA,TOTB); cnt:=0; n:=-1; - fori:=1To Tota Do101 begin102Read (x); forj:=1to X DoBegin Inc (n); Read (A[n]); Inc (A[n],inf);p os[n]:=I;end;103Inc (CNT); Inc (n); a[n]:=CNT;104Read (x); forj:=1to X DoBegin Inc (n); Read (A[n]); Inc (A[n],inf);p os[n]:=I;end; theInc (CNT); Inc (n); a[n]:=CNT;106 end;107 fori:=1To TOTB Do108 begin109ter[i]:=n+1; theRead (x); forj:=1to X DoBegin Inc (n); Read (A[n]); Inc (A[n],inf); end;111len[i]:=x; theInc (CNT); Inc (n); a[n]:=CNT;113 end; theFillchar (Vis,sizeof(VIS),255); the Inc (n); the Suffix_array;117 118 Calc_height;119Fillchar (ans,sizeof(ANS),0); - fori:=1To TOTB DoWriteln (Solve (ter[i],len[i));121 fori:=1To tota-1 DoWrite (Ans[i],' '); Writeln (Ans[tota]);122End.
[BZOJ2754] [SCOI2012] A name-solving report on the Meow Planet | suffix array