Secret Message
Time limit1sMemory limit128 MB
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 () bits of each of () secret binary messages.
He has also compiled a list of () partial codewords that he believes the cows are using. For codeword he only knows its first () 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 , determine how many of the intercepted messages match it.
The total number of bits in the input (the sum of all and all ) does not exceed .
Input
- Line 1: two integers and .
- Lines 2 to : line describes intercepted message as an integer followed by space-separated bits (each or ).
- Lines to : line describes codeword as an integer followed by space-separated bits (each or ).
Output
- Lines 1 to : line contains a single integer, the number of intercepted messages that match codeword .
Hint
Consider the four messages , , , and the five codewords , , , , .
- Codeword matches only : match.
- Codeword matches , , and : matches.
- Codeword matches only : match.
- Codeword matches only (the message is a prefix of it): match.
- Codeword matches and : matches.