2754: [SCOI2012] The name on the Meow Planet time limit: Sec Memory Limit: MB
Submit: 805 Solved: 380
[Submit] [Status] [Discuss] Descriptiona180285 was fortunate to have been chosen as an international student for the Earth to meow. He found that the cat's name before class was notoften interesting. Suppose there are n cats in the class, and each meow name consists of a surname and a name. The teacher on the Mew planet will choosem-String to call, each time a string is read out, if the string is a MEW surname or a substring of the name, thenthe meow must answer. However, because the loadline of the Mew are too eccentric to be expressed in ASCII. For convenience of description,a180285 decided to use several strings to denote the name of the Mew. Now you can help a180285 to count the number of cats answered at each call, and the end of the M-roll.How many times does each meow answer? Inputnow define the string given method on the Meow Planet:A positive integer L is given, representing the length of the string, followed by an L integer representing 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 names are standard meow on the planetstring. next there are M lines, each containing a string on the Meow planet, indicating the string the teacher named. Output for each teacher named string output How many cats should answer. 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
1 2
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.
Source
This question is an array of suffixes.
First write the pattern string p as S11*S12*S21*S22...SN2 (* for special characters)
Then use Find to count the upper and lower bounds in the SA (Len Total loglen total +mloglen total) Worst O (mlen total)
May be t but the solution is still unknown.
#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include < functional> #include <iostream> #include <cmath> #include <cctype> #include <ctime>using namespace std; #define for (I,n) for (int. i=1;i<=n;i++) #define FORK (I,k,n) for (int. i=k;i<=n;i++) #define REP (I,n) for (int i=0;i<n;i++) #define ForD (I,n) for (int. i=n;i;i--) #define REPD (I,n) for (int. i=n;i>=0;i--) #define FORP (x) for ( int p=pre[x];p; p=next[p]) #define FORPITER (x) for (int &p=iter[x];p; p=next[p]) #define LSON (x<<1) #define Rson ((x<<1) +1) #define MEM (a) memset (A,0,sizeof (a)), #define MEMI (a) memset (A,127,sizeof (a)), #define MEMI (a) memset ( A,128,sizeof (a)); #define INF (2139062143) #define F (100000007) #define MAXN (1000000+10) #define SIGMA_SIZE (10000+10) # Define SP_CHAR (10001) typedef long Long Ll;ll Mul (ll A,ll b) {return (a*b)%F;} ll Add (ll A,ll b) {return (a+b)%F;} ll Sub (ll A,ll b) {return (a-b+ (a)/f*f+f)%F; void Upd (ll &a,ll b) {a= (a%f+b%f)%F;}Convention -1:/0sp_char:*cat[i]=0 the position no cat class Sa{public:int S[maxn];int Sa[maxn],t[maxn],t2[maxn],c[maxn],n;sa () {}SA (int *_ S,int _n) {memcpy (s,_s,sizeof (s)); n=_n; MEM (sa) mem (t) mem (T2) mem (c)}void mem (int *_s,int _n) {memcpy (s,_s,sizeof (s)); n=_n; MEM (sa) mem (t) mem (T2) mem (c)}void Build_sa (int m) {int *x=t,*y=t2; Rep (i,m) c[i]=0; Rep (i,n) c[x[i]=s[i]]++; X[i] I string first keyword row for (i,m-1) c[i]+=c[i-1]; REPD (i,n-1) sa[--c[x[i]]]=i;for (int k=1;k<=n;k<<=1) {int p=0; Fork (i,n-k,n-1) y[p++]=i; REP (I,n) if (sa[i]>=k) y[p++]=sa[i]-k; Y[i] Second keyword row i is the first? String Rep (i,m) c[i]=0; Rep (i,n) c[x[y[i]]]++; X[y[i]] The string of the second keyword row i, the 1th keyword row for (i,m-1) c[i]+=c[i-1]; REPD (i,n-1) sa[--c[x[y[i]]]]=y[i]; The second keyword from large to small traversal, sorted by the first keyword, and finally get the 1, 2 keyword sort of//At this time SA is the first keyword, the 2nd keyword are ranked in the order of swap (x, y); At this point y becomes rank of the first keyword sort, and the goal is to turn X into the first, two-key sort of rank p=1; x[sa[0]]=0; for (i,n-1) x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i]+k]==y[sa[i-1]+k]? P-1:p++;if (P>=n) break;m=p;}} int m; The length of the template string P must be assigned in advance int cmp_suffix (int *pattern,int p) {for (int i=0;i<m;i++) {if (Pattern[i]!=s[sa[p]+i]) return pattern[i]-s[sa[p]+i];} return 0; }int Find (int *p,int _m) {m=_m;//Assignment Here is also OK if (Cmp_suffix (p,0) <0| | Cmp_suffix (p,n-1) >0) return-1;int L=0,r=n-1;while (l<=r) {int m= (l+r) >>1;int res=cmp_suffix (P,M); if (!res ) return M;else if (res<0) R=m-1;else l=m+1;} return-1;} int rank[maxn],height[maxn];void make_height () {int k=0; Rep (i,n) rank[sa[i]]=i; Rep (i,n) {if (rank[i]-1<0) continue;if (k) k--;int J=sa[rank[i]-1];while (s[i+k]==s[j+k]) ++k;height[rank[i]]=k;}} S;int flag[maxn]={0},cat[maxn]={0},sum[maxn]={0};int len,s[maxn];int n,m;int get_init (int &p,int i,bool is_cat) { int len;scanf ("%d", &len), while (len--) {scanf ("%d", &s[++p]), if (Is_cat) cat[p]=i;} return Len; }int Main () {//freopen ("bzoj2754.in", "R", stdin),//freopen (". Out", "w", stdout); scanf ("%d%d", &n,&m); int P=-1 ; for (I,n) {for (j,2) {get_init (p,i,1); s[++p]=sp_char;cat[p]=0;}} S[++p]=-1; S.mem (S,P); S.build_sa (sigma_size); S.make_height (); for (i,m) {p=-1;get_init (p,i,0); s[++p]=-1;inT K=s.find (s,p); if (k==-1) {cout<< "0\n"; continue;} int Ans=0;int t=s.sa[k];if (flag[cat[t]]^i) sum[cat[t]]++,flag[cat[t]]=i,++ans;int K=k;while (k>0&&S.cmp_ Suffix (s,k-1) ==0) {K--;int t=s.sa[k]; if (flag[cat[t]]^i) Sum[cat[t]]++,flag[cat[t]]=i,++ans;} K=k;while (K<s.n&&s.cmp_suffix (s,k+1) ==0) {K++;int t=s.sa[k]; if (flag[cat[t]]^i) sum[cat[t]]++,flag[cat[ T]]=i,++ans;} printf ("%d\n", ans);} printf ("%d", sum[1]); Fork (i,2,n) printf ("%d", Sum[i]);p utchar (' \ n '); return 0;}
Bzoj 2754 ([SCOI2012] The name on the Meow planet-suffix array statistic sequence set number of occurrences of a neutron sequence)