Switch Array
Time limit1sMemory limit256 MB
Find the fewest restricted toggles that turn each given bit string into all zeros.
- Level
Medium7 of 10
- Topics
- Recursion, Dynamic programming, Math
- Solved
- No attempts yet
Problem
A new locking device has been built.
The device consists of switches , each of which is either open or closed. The state of a switch is 0 or 1, so the state of the device is an array of bits.
The table below shows an example array with 8 switches.
In an array of switches, the rules for operating a switch are as follows.
- Rule 1) You can toggle at any time to change its state.
- Rule 2) When is 1 and are all 0, you can toggle to change its state. This rule also applies when the switches do not exist at all. For example, you can toggle as long as is 1.
- Rule 3) You can toggle only one switch at a time.
The device opens when every switch is 0.
Following the rules above, find the minimum number of toggles that turns the given array into all zeros.
The table below is a shortest sequence that turns the array 1111 into 0000. The array 1111 needs at least 10 toggles.
Input
The first line contains the number of test cases .
Each test case follows on its own line as a bit string . The first bit of is and the last bit is .
The length of satisfies .
Output
For each test case, print on one line the minimum number of toggles needed to turn every switch to 0.