Orig Description
Snake Escaping
At the JOI research institute, there are 2^L venomous snakes, each with a number 0, 1, ..., 2^L - 1. All venomous snakes are divided into L segments from the head, and each segment is either blue or red. For venomous snake i, let i be the binary string: i =$\sum_{k=1}^{L}$ c_k2^{L-k} (0 \leq c_k \leq 1) (0 \leq c_k \leq 1).
If c_k = 0, then the kth segment from the head of venomous snake i is blue.
If c_k = 1, then the kth segment from the head of venomous snake i is red.
Each venomous snake has a toxicity, which is an integer value greater than or equal to 0 and less than or equal to 9. A length 2^L string S consisting of 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 is given, with the ith character (1 \leq i \leq 2^L) representing the toxicity of venomous snake i - 1.
Snakes are fast-moving, and often escape from the JOI research institute. Residents in the surrounding areas lodge complaints after witnessing escaped venomous snakes.
You are given information on complaints over Q days.d-th day's complaints (1 <= d <= Q) are represented as a length L string T_d consisting of 0, 1, and ?.
If the jth character (1 <= j <= L) of T_d is 0, it means that all escaped venomous snakes on day d have a blue segment at the jth position.
If the jth character (1 <= j <= L) of T_d is 1, it means that all escaped venomous snakes on day d have a red segment at the jth position.
If the jth character (1 <= j <= L) of T_d is ?, it means that no information was provided by residents regarding the jth segment of the escaped venomous snakes on day d.
All complaints contain accurate information. Escaped venomous snakes are captured by the JOI research institute staff on the same day. Captured venomous snakes may escape again on subsequent days.
In order to estimate the risk of venomous snake escaping, Mr. K, the director of the JOI research institute, wants to know the total toxicity of possibly escaped venomous snakes. Your task is to create a program that, from the information of complaints over Q days, calculates the total toxicity of possibly escaped venomous snakes for each day.
Task
Given a string S that represents the toxicity of venomous snakes and information on complaints over Q days, create a program to calculate the total toxicity of potentially escaped venomous snakes for each day.
Keep in mind the small memory limit.
Input
Read the following input from standard input:
In the first line, the integers L and Q are written separated by a space. These represent, in order, the number of venomous snake segments and the number of days when complaints are lodged.
In the second line, a length 2^L string S is written. This string represents the toxicity of venomous snakes.
In the following Q lines, the length L string T_d is written in the dth line (1 <= d <= Q). This string represents the complaint on day d.
Output
Print the output on Q lines. On the dth line, print the integer representing the total toxicity of potentially escaped venomous snakes on day d.
Constraints
All input data meets the following conditions:
1 \leq L \leq 20
1 \leq Q \leq 1 000 000
S is a length 2^L string.
The string S consists of the characters 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.
T_d is a length L string (1 <= d <= Q).
The string T_d consists of the characters 0, 1, and ? (1 <= d <= Q).
Example Input and Output
Example Input 1
3 5
12345678
000
0??
1?0
?11
???
Example Output 1
1
10
12
12
36
Extracted Specification
An integer L (1 ≤ L ≤ 20) and an integer Q (1 ≤ Q ≤ 1,000,000) separated by a space.
A string S of length 2^L consisting of characters {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}.
Q strings, each of length L, consisting of characters {'0', '1', '?'}.
### Example Input:
```
3 5
12345678
000
0??
1?0
?11
???
```
### Function Signature:
Write a function f(L, Q, S, complaints) that takes in the input.
def f(L: int, Q: int, S: str, complaints: List[str]):
'''
L: an integer
Q: an integer
S: a string
complaints: a list of strings
'''
Test Case Generator
Sure, let's write a test case generator for the "Snake Escaping" problem. We'll need to generate random test cases based on the problem constraints:
1. An integer \( L \) (1 ≤ \( L \) ≤ 20).
2. An integer \( Q \) (1 ≤ \( Q \) ≤ 1,000,000).
3. A string \( S \) of length \( 2^L \) consisting of digits 0 through 9.
4. \( Q \) strings \( T_d \) of length \( L \) consisting of characters '0', '1', and '?'.
Here is the Python code for the test case generator:
```python
import random
def generate_test_case():
# Generate L and Q
L = random.randint(1, 20)
Q = random.randint(1, 1000000)
# Generate S of length 2^L with digits 0-9
S_length = 2 ** L
S = ''.join(random.choice('0123456789') for _ in range(S_length))
# Generate Q strings T_d of length L with characters '0', '1', and '?'
T_d_list = []
for _ in range(Q):
T_d = ''.join(random.choice('01?') for _ in range(L))
T_d_list.append(T_d)
return L, Q, S, T_d_list
# Function to print generated test cases in the required format
def print_test_case(L, Q, S, T_d_list):
print(L, Q)
print(S)
for T_d in T_d_list:
print(T_d)
# Example usage
if __name__ == "__main__":
L, Q, S, T_d_list = generate_test_case()
print_test_case(L, Q, S, T_d_list)
```
### Explanation:
1. **Generating \( L \) and \( Q \)**:
- \( L \) is selected randomly between 1 and 20.
- \( Q \) is selected randomly between 1 and 1,000,000.
2. **Generating \( S \)**:
- The length of \( S \) is \( 2^L \).
- Each character in \( S \) is randomly chosen from the digits '0' to '9'.
3. **Generating \( T_d \)**:
- For each day \( d \), a string \( T_d \) of length \( L \) is generated.
- Each character in \( T_d \) is randomly chosen from '0', '1', and '?'.
The `generate_test_case` function creates these values and the `print_test_case` function prints them in the format expected by the problem statement.
You can use this generator to create random test cases and validate your solution against different inputs.
Extract Arguments
def extract_arguments(fh):
L, Q = map(int, fh.readline().strip().split())
S = fh.readline().strip()
T = [fh.readline().strip() for _ in range(Q)]
return L, Q, S, T
# if __name__ == "__main__":
# input_path = sys.argv[1]
# with open(input_path, 'r') as fh:
# L, Q, S, T = extract_arguments(fh)
# f(L, Q, S, T)