Bzoj 2754 [SCOI2012] Meow name on the planet (suffix array)

Source: Internet
Author: User

2754: [SCOI2012] The name on the Meow Planet time limit:20 Sec Memory limit:128 MB
submit:1359 solved:618
[Submit] [Status] [Discuss] Descriptiona180285 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? Input now defines a string on the Meow planet given a method: first give a positive integer l, which represents the length of the string, and the next L integer represents each character of the string. The first line of input is two integers n and M. Next there are n rows, each containing the first and last names of the I-Cat and two strings. The surname and the name are the standard meow strings on the planet. Next there are m lines, each containing a string on the Meow planet, indicating the string the teacher named. Output how many cats should be answered by the number of strings each teacher has named. The last line then outputs how many times each meow is ordered. Sample Input2 3
6 8 25 0 24 14 8 6 18 0 10 20 24 0
7 14 17 8 7 0 17 0 5 8 25 0 24 0
4 8 25) 0 24
4 7 0) 17 0
4 17 0) 8 25
Sample Output
2
1
0
0 S
Prompted
The fact that the data given in the sample is translated into the language of the earth can be seen
2 3
Izayoi Sakuya
Orihara Izaya
Izay
Hara
RaizHINT



"Data Range"

For 30% of data, ensure that:

1<=n,m<=1000, the names of the cats are not more than 4000, the total length of the name string does not exceed 2000.

For 100% of data, ensure that:

1<=n<=20000,1<=m<=50000, the total length of the cat's name and the total length of the count are not more than 100000, ensuring that the number of characters in the Meow string is not more than 10000.

Ideas

Suffix array

Concatenate all of them, including names and inquiries. Processing out rank[],sa[],height[], through rank to determine the location of a query, and then on the height of the left and right to sweep, statistics can be. Flag on the same mark, Kase is a timestamp (also known as =-=)

Code

1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5 using namespacestd;6 7 Const intN =3*1e5+1e2;8 9 intS[n];Ten intSa[n],height[n],rank[n],t[n],t2[n],c[n]; One  A voidBuild_sa (intMintN) { -     inti,k,*x=t,*y=T2; -      for(i=0; i<m;i++) c[i]=0; the      for(i=0; i<n;i++) c[x[i]=s[i]]++; -      for(i=0; i<m;i++) c[i]+=c[i-1]; -      for(i=n-1; i>=0; i--) sa[--c[x[i]]]=i; -      for(k=1; k<=n;k<<=1) { +         intp=0; -          for(i=n-k;i<n;i++) y[p++]=i; +          for(i=0; i<n;i++)if(sa[i]>=k) y[p++]=sa[i]-K; A          for(i=0; i<m;i++) c[i]=0; at          for(i=0; i<n;i++) c[x[y[i]]]++; -          for(i=0; i<m;i++) c[i]+=c[i-1]; -          for(i=n-1; i>=0; i--) sa[--c[x[y[i]]]]=Y[i]; - swap (x, y); -p=1; x[sa[0]]=0; -          for(i=1; i<n;i++)  inx[sa[i]]=y[sa[i]]==y[sa[i-1]]&&y[sa[i]+k]==y[sa[i-1]+K]? P1:p + +; -         if(p>=n) Break; tom=p; +     } - } the voidGet_height (intN) { *     inti,j,k=0; $      for(i=0; i<n;i++) rank[sa[i]]=i;Panax Notoginseng      for(i=0; i<n;i++) { -         if(k) k--; thej=sa[rank[i]-1]; +          while(S[i+k]==s[j+k]) k++; Aheight[rank[i]]=K; the     } + } -  $ intn,m; $ intFlag[n],kase,ans[n], from[n],que[n],length[n]; - voidReadint&x) { -     CharC=getchar ();intf=1; x=0; the      while(!isdigit (c)) {if(c=='-') f=-1; C=GetChar ();} -      while(IsDigit (c)) x=x*Ten+c-'0', c=GetChar ();Wuyix*=F; the } - intMain () { WuMemset from,-1,sizeof( from)); - read (n), read (m); About     intx,len=0; $      for(intI=0; i<n;i++) { - read (x); -          for(intj=0; j<x;j++) -Read (S[len]), from[len++]=i; As[len++]=10001; + read (x); the          for(intj=0; j<x;j++) -Read (S[len]), from[len++]=i; $s[len++]=10001; the     } the      for(intI=0; i<m;i++) { the read (x); theQue[i]=len; length[i]=x; -          for(intj=0; j<x;j++) inRead (s[len++]); thes[len++]=10001; the     } AboutBuild_sa (10002, Len); the Get_height (len); the      for(intI=0; i<m;i++) { the         intp=rank[que[i]],tot=0; +++Kase; -          while(height[p]>=Length[i]) { the             if( from[sa[p-1]]!=-1)Bayi                 if(flag[ from[sa[p-1]]]!=Kase) { theflag[ from[sa[p-1]]]=Kase; the++tot; -++ans[ from[sa[p-1]]]; -                 } thep--; the             if(!p) Break; the         } thep=Rank[que[i]]; -          while(height[p+1]>=Length[i]) { the             if( from[sa[p+1]]!=-1) the                 if(flag[ from[sa[p+1]]]!=Kase) { theflag[ from[sa[p+1]]]=Kase;94++tot; the++ans[ from[sa[p+1]]]; the                 } thep++;98             if(P==len) Break; About         } -printf"%d\n", tot);101     }102printf"%d", ans[0]);103      for(intI=1; i<n;i++) printf ("%d", Ans[i]);104}

Bzoj 2754 [SCOI2012] Meow name on the planet (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.