Title Description
Bessie was leading the cows to flee. In order to contact, the cows send each other secret information. The information is binary, with a total of M (1≤m≤50000) bars. A strong anti-espionage John has partially intercepted this information, knowing the pre-bi (l bi≤10000) bit of the binary information of article I. He also knows that cows use N (1≤n≤50000) codes. However, he is only aware of the former CJ (1≤cj≤10000) Bit of the code of Article J. For each password J, he wanted to know how much of the truncated information would match it. That is, how much information has the same prefix as this password. Of course, the prefix length must be equal to the password and the smaller of the length of the message. In the input file, the total number of bits (that is, ∑bi+∑ci) does not exceed 500000.
Input
Line 1th enters N and M, followed by n lines to describe the secret information, followed by the M line to describe the password. Enter an integer for each line to indicate the length of the information or password, and then enter this information or password. All numbers are separated by a space.
Output
Total m-line, output the number of matching information for each password.
Sample input
Sample output
Exercises
First build the information into a dictionary tree. Mark the end of each message and record how many are at the end of the node. When querying, if the prefix equals information, the number of tokens passed in the dictionary tree is returned, and if the prefix equals the password, then the number of tokens in the subtree of the last character of the password is returned.
#include <cmath>#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;#definell Long LongConst intmaxn=50000*Ten;intt[maxn][2],N,M,LEN,ROOT,TOT,CNT[MAXN],NUM[MAXN];Charc[500000+ -];BOOLWORD[MAXN];voidInsertChar*s,intR) {Root=0;intID; for(intI=0; i<r;i++) {ID=s[i]-'0'; if(!t[root][id]) t[root][id]=++tot; Cnt[root]++;root=T[root][id]; } Word[root]=true; num[root]++;}intFindChar*s,intR) {Root=0;intid,ans=0; for(intI=0; i<r;i++) {ID=s[i]-'0'; if(!T[root][id]) { returnans; } Root=t[root][id];if(Word[root]) ans+=Num[root]; } returnans+cnt[root];} Template<typename t>voidRead (t&AA) { Charcc ll Ff;aa=0; Cc=getchar (); ff=1; while((cc<'0'|| Cc>'9') &&cc!='-') cc=GetChar (); if(cc=='-') ff=-1, cc=GetChar (); while(cc>='0'&&cc<='9') aa=aa*Ten+cc-'0', cc=GetChar (); AA*=ff;}intMain () {read (M), read (n); for(intI=1; i<=m;i++){ intx,w; Read (w); for(intI=1; i<=w;i++) Read (x), c[i-1]=x+'0'; Insert (C,W); } for(intI=1; i<=n;i++){ intx,w; Read (w); for(intI=1; i<=w;i++) Read (x), c[i-1]=x+'0'; printf ("%d\n", find (c,w)); } return 0;}
"bzoj1590" "Usaco2008 Dec" secret Messages secret Message