P2922 [USACO08DEC] Secret Message

Topic description

Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret binary messages to each other.

Ever the clever counterspy, Farmer John has intercepted the first b_i (1 <= b_i <= 10,000) bits of each of M (1 <= M <= 50,000) of these secret binary messages.

He has compiled a list of N (1 <= N <= 50,000) partial codewords that he thinks the cows are using. Sadly, he only knows the first c_j (1 <= c_j <= 10,000) bits of codeword j.

For each codeword j, he wants to know how many of the intercepted messages match that codeword (i.e., for codeword j, how many times does a message and the codeword have the same initial bits). Your job is to compute this number.

The total number of bits in the input (i.e., the sum of the b_i and the c_j) will not exceed 500,000.

Memory Limit: 32MB

POINTS: 270

Bessie is leading the cows to escape. To communicate, the cows send secret messages to each other.

The information is binary, and there are M (1≤M≤50000) pieces in total. John, who has a strong anti-espionage ability, has partially intercepted this information and knows the first bi (l<bi≤10000) bits of the i-th binary information. He also knows that cows use N (1≤N≤50000) passwords. However, he only knows the first cj (1≤cj≤10000) bits of the J-th cipher.

For each password J, he wants to know how many intercepted messages can match it. That is, how many messages have the same prefix as this password. Of course, the prefix length must be equal to the lesser of the password and the length of the message.

In the input file, the total number of bits (i.e. ∑Bi+∑Ci) will not exceed 500000.

Input and output format

Input format:

 

* Line 1: Two integers: M and N

* Lines 2..M+1: Line i+1 describes intercepted code i with an integer b_i followed by b_i space-separated 0's and 1's

* Lines M+2..M+N+1: Line M+j+1 describes codeword j with an integer c_j followed by c_j space-separated 0's and 1's

 

Output format:

 

* Lines 1..M: Line j: The number of messages that the jth codeword could match.

 

Input and output example

Input Example #1:  Copy
4 5
3 0 1 0
1 1
3 1 0 0
3 1 1 0
1 0
1 1
2 0 1
5 0 1 0 0 1
2 1 1
Output Sample #1:  Copy
1
3
1
1
2

illustrate

Four messages; five codewords.

The intercepted messages start with 010, 1, 100, and 110.

The possible codewords start with 0, 1, 01, 01001, and 11.

0 matches only 010: 1 match

1 matches 1, 100, and 110: 3 matches

01 matches only 010: 1 match

01001 matches 010: 1 match

11 matches 1 and 110: 2 matches

 

 

//01 Trie

// Record the number of pieces of information passing through the path cnt (not ending here, not the same as the path), and the number of pieces of information ending here (the same number of pieces of information as the path) 
 // Then, when querying ,
 // ans+=sum of each point along the path 
 // if the password can be successfully matched, let ans+=root->cnt
 // that is, add the same number of pieces of information as the password prefix
 // if the password cannot be successfully matched 
 / / Just return ans directly 

#include <iostream> 
#include <cstdio> 
#include <algorithm>
 using  namespace std;

const int N=5e5+5;

int n,m,len;
struct TREE
{
    int num
    int cnt
    TREE *son[2];
}tree[N];

typedef TREE* Tree;
Tree Root,now_node,null;

inline int read()
{
    char c=getchar();int num=0;
    for(;!isdigit(c);c=getchar());
    for(;isdigit(c);c=getchar())
        num = num * 10 + c - ' 0 ' ;
    return num;
}

void init()
{
    Root=now_node=null=tree;
    null->son[1]=null->son[0]=null;
}

inline Tree new_node(int num)
{
    ++now_node;
    now_node->num=num;
    now_node->son[0]=now_node->son[1]=null;
    return now_node;
}

void insert(Tree root)         // Insert information 
{
     for ( int i= 1 ,a;i<=len;++ i)
    {
        a=read();
        if(root->son[a]==null)
            root->son[a]=new_node(a);
        root =root-> son[a];
         ++(root->cnt);         // Number of messages passing through the path +1 
    }
     --(root->cnt);         // Subtract the exact same 
    ++( root->sum);         // This piece of information +1 
}

int query(Tree root)
{
    int ans=0;
    bool ok=1;
    for(int i=1,a;i<=len;++i)
    {
        a = read();
         if (ok== 0 ||root->son[a]== null )     // Do not return early because the password has not been read yet 
        {
            ok=0;
            continue;
        }
        root=root->son[a];
        ans +=root->sum;         // Add the number of messages shorter than the password, they are completely included in the password or the same as the password 
    }
     if (ok)     // The password matches successfully 
        return ans+root->cnt;         // Add the same number of messages as the password prefix 
    return ans;
}

intmain ()
{
//    freopen("testdata.in","r",stdin);
//    freopen("233.out","w",stdout);
    init();
    m=read(),n=read();
    for(int i=1;i<=m;++i)
    {
        len=read();
        insert(Root);
    }
    for(int i=1;i<=n;++i)
    {
        len=read();
        printf("%d\n",query(Root));
    }
    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325153779&siteId=291194637