CF2103B.Binary Typewriter

传统题 时间 2000 ms 内存 256 MiB 5 尝试 1 已通过 1 标签

Binary Typewriter

You are given a binary string ss of length nn and a typewriter with two buttons: 0 and 1. Initially, your finger is on the button 0. You can do the following two operations:

  1. Press the button your finger is currently on. This will type out the character that is on the button.
  2. Move your finger to the other button. If your finger is on button 0, move it to button 1, and vice versa.

The cost of a binary string is defined as the minimum number of operations needed to type the entire string.

Before typing, you may reverse at most one substring^{\text{∗}} of ss. More formally, you can choose two indices 1lrn1\le l\le r\le n, and reverse the substring slrs_{l\ldots r}, resulting in the new string $s_1s_2\ldots s_{l-1}s_rs_{r-1}\ldots s_ls_{r+1}\ldots s_n$.

Your task is to find the minimum possible cost among all strings obtainable by performing at most one substring reversal on ss.

^{\text{∗}}A string aa is a substring of a string bb if aa can be obtained from bb by the deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains a single integer nn (1n21051\le n\le 2\cdot 10^5) — the length of the binary string ss.

The second line of each test case contains a binary string s1s2sns_1s_2\ldots s_n (si=0s_i = \mathtt{0} or si=1s_i = \mathtt{1}) — the characters of the binary string ss.

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10^5.

Output

For each test case, output the minimum cost of string ss after performing at most one substring reversal.

Note

In the first test case, we can choose not to reverse any substrings. We can do operation 11 three times to type 000.

In the second test case, we can choose not to reverse any substrings. We can do operation 22 to move our finger to button 1. Then, we do operation 11 three times to type 111.

In the third test case, we can choose not to reverse any substring. We can do operation 11 to type 0. Then, we do operation 22 to move our finger to button 1. Finally, we do operation 11 two times to type 11, resulting in the final string 011 using only 44 operations.

In the fourth test case, we can reverse the substring s13s_{1\ldots 3}, resulting in the string 001. We can do operation 11 two times to type 00. Then we do operation 22 to move our finger to button 1. Finally, we do operation 11 once to type 1, resulting in the final string 001 using only 44 operations.

In the fifth test case, we can reverse the substring s23s_{2\ldots 3}, resulting in the string 11001. The cost of the string is 88 as we can do the following sequence of operations:

  • Do operation 22 to move our finger to button 1.
  • Do operation 11 two times to type 11.
  • Do operation 22 to move our finger to button 0.
  • Do operation 11 two times to type 00.
  • Do operation 22 to move our finger to button 1.
  • Do operation 11 once to type 1.

In the sixth test case, we can reverse the substring s517s_{5\ldots 17}, resulting in the string 1101111011001001000. It can be proven that the minimum number of operations needed to type the binary string is 2929.

Samples

6
3
000
3
111
3
011
3
100
5
10101
19
1101010010011011100
3
4
4
4
8
29

在线编程 IDE

建议全屏模式获得最佳体验