#K76172. Toggle Devices
Toggle Devices
Toggle Devices
You are given several devices, each represented by a binary string. Your task is to update the state of these devices by applying a series of toggle operations. For each update, a toggle mask is provided: if the mask has a '1' in any position, the corresponding bit in the device's state is flipped (i.e., 0 becomes 1 and 1 becomes 0). Apply all updates in the given order and output the final states of the devices.
The problem involves simulating a bitwise toggle operation on strings. More formally, if a device has initial state \(S\) and you apply an update with toggle mask \(M\) (both are binary strings of equal length), then the resulting state \(S'\) is computed as follows:
\[ S'_i = \begin{cases} 1 - S_i, & \text{if } M_i = 1, \\ S_i, & \text{if } M_i = 0. \end{cases} \]
Input consists of multiple test cases. For each test case, you will be given the number of devices, the initial state of each device, the number of update operations, and the update operations themselves. Each update operation specifies which device to update (using 0-indexing) and the corresponding toggle mask.
inputFormat
The input is provided from stdin in the following format:
T D state1 state2 ... (D states) U i toggle_mask ... (U lines of updates)
Where:
- T is the number of test cases.
- For each test case:
- D is the number of devices.
- Then D lines follow, each containing a binary string representing the initial state of a device.
- U is the number of update operations.
- Then U lines follow, each containing an integer
i
(0-indexed) and a binary stringtoggle_mask
, separated by a space.
outputFormat
For each test case, output the final state of each device on a separate line to stdout. The outputs of consecutive test cases are printed sequentially (i.e. there is no special separator between test cases).
## sample4
2
0101
1100
3
0 1001
1 0101
0 1111
1
0000
1
0 1111
3
0000
1111
1010
3
0 1111
1 0000
2 1010
2
0101
1010
0
0011
1001
1111
1111
1111
0000
0101
1010
</p>