#C10956. Minimum Operations to Equalize Binary String

    ID: 40218 Type: Default 1000ms 256MiB

Minimum Operations to Equalize Binary String

Minimum Operations to Equalize Binary String

You are given a binary string B of length N consisting only of the characters '0' and '1'. Your task is to determine the minimum number of operations required to make all characters of the string equal. In one operation, you can choose a contiguous segment of consecutive '0's and change them all to '1's.

If the string already contains no '0's, then no operations are needed. Otherwise, the answer is the number of contiguous segments of '0's in the string.

Formally, if we denote the binary string as B, let the number of contiguous substrings consisting of one or more 0's be k. Then the minimum number of operations required is:

$$\text{operations} = \begin{cases} 0, & \text{if } B \text{ has no } 0 \\ k, & \text{otherwise} \end{cases} $$

Note that even if the entire string consists of zeros, it is considered as one contiguous segment.

inputFormat

The input is given via stdin and has the following format:

  • The first line contains a single integer T, the number of test cases.
  • Each of the following T lines contains a test case described by an integer N and a binary string B of length N, separated by a space.

You can assume that the input is valid.

outputFormat

For each test case, print the minimum number of operations required on a separate line to stdout.

## sample
2
7 1000001
5 11111
1

0

</p>