This page is still under construction.

Parts of this page are still being built. What you see may change.

Secret Message

Time limit1sMemory limit128 MB

Summary
Given M binary messages and N binary codewords, count for each codeword how many messages share a prefix relation with it in either direction.
Level

Medium6 of 10

Topics
Trie, String, Prefix sum, DFS
Solved
No attempts yet

Problem

Bessie is leading the cows in an attempt to escape, and to coordinate they send each other secret binary (0 and 1) messages.

A counterspy has intercepted the first bib_i (1≤bi≤1041 \le b_i \le 10^4) bits of each of MM (1≤M≤5×1041 \le M \le 5 \times 10^4) secret binary messages.

He has also compiled a list of NN (1≤N≤5×1041 \le N \le 5 \times 10^4) partial codewords that he believes the cows are using. For codeword jj he only knows its first cjc_j (1≤cj≤1041 \le c_j \le 10^4) bits.

A message and a codeword match when one is a prefix of the other: reading from the first bit, they agree on every bit up to the length of the shorter of the two. For each codeword jj, determine how many of the MM intercepted messages match it.

The total number of bits in the input (the sum of all bib_i and all cjc_j) does not exceed 5×1055 \times 10^5.

Input

  • Line 1: two integers MM and NN.
  • Lines 2 to M+1M+1: line i+1i+1 describes intercepted message ii as an integer bib_i followed by bib_i space-separated bits (each 00 or 11).
  • Lines M+2M+2 to M+N+1M+N+1: line M+j+1M+j+1 describes codeword jj as an integer cjc_j followed by cjc_j space-separated bits (each 00 or 11).

Output

  • Lines 1 to NN: line jj contains a single integer, the number of intercepted messages that match codeword jj.

Hint

Consider the four messages 010010, 11, 100100, 110110 and the five codewords 00, 11, 0101, 0100101001, 1111.

  • Codeword 00 matches only 010010: 11 match.
  • Codeword 11 matches 11, 100100, and 110110: 33 matches.
  • Codeword 0101 matches only 010010: 11 match.
  • Codeword 0100101001 matches only 010010 (the message 010010 is a prefix of it): 11 match.
  • Codeword 1111 matches 11 and 110110: 22 matches.

Examples4

  1. Example 1

    Input
    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
    
    Expected output
    1
    3
    1
    1
    2
    
  2. Example 2

    Input
    1 1
    1 0
    1 0
    
    Expected output
    1
    
  3. Example 3

    Input
    1 1
    3 1 0 1
    1 1
    
    Expected output
    1
    
  4. Example 4

    Input
    1 1
    1 0
    1 1
    
    Expected output
    0