#SRD0000138. Rắn độc đào thoát (Snake Escaping)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Rắn độc đào thoát (Snake Escaping)
Snake Escaping
Source: Japanese Olympiad in Informatics
Version: Phuoc Hung OJ Extended
Problem Statement
The JOI Laboratory keeps poisonous snakes numbered from through . Each snake has exactly body parts, ordered from head to tail. Every part is either blue or red.
Write snake in binary using exactly digits, from the most significant to the least significant bit. A 0 denotes a blue part, whereas a 1 denotes a red part.
Every snake has an integer toxicity between and . The string has length ; its character at position (one-based string indexing) gives the toxicity of snake .
The laboratory receives reports for days. The report for day is a string with characters. For each body part:
0requires the part to be blue;1requires the part to be red;?provides no information about the color of the part.
All reports are accurate. Escaped snakes are captured the same day and may escape again on subsequent days. Thus, each day's report is considered independently of previous days.
For each day, compute the sum of toxicities of all snakes whose identifiers are consistent with that day's report. Count each matching snake exactly once.
Input
- The first line contains integers : the number of body parts per snake and the number of reporting days.
- The second line contains string of exactly digits, giving the toxicities in increasing snake-identifier order.
- Each of the next lines contains a length- string describing the report for day .
Output
Print lines. Line contains one integer: the sum of toxicities of all snakes matching .
Subtasks
- Subtask 1 (5 points): ; ; ; contains digits
0–9; each has length and contains only0,1,?. - Subtask 2 (7 points): ; ; ; contains digits
0–9; each has length and contains only0,1,?. - Subtask 3 (10 points): ; ; ; contains digits
0–9; each has length and contains only0,1,?. - Subtask 4 (53 points): ; ; ; contains digits
0–9; each has length and contains only0,1,?. - Subtask 5 (25 points): ; ; ; contains digits
0–9; each has length and contains only0,1,?.
Examples
Example 1
Input
3 5
12345678
000
0??
1?0
?11
???
Output
1
10
12
12
36
Explanation
There are snakes numbered through , with toxicities . For 000, only snake matches: . For 0??, snakes match: . For 1?0, snakes (100) and (110) match: . For ?11, snakes and match: . Finally, ??? matches all eight snakes, giving .