欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF2103B.Binary Typewriter
Binary Typewriter
You are given a binary string of length and a typewriter with two buttons: 0 and 1. Initially, your finger is on the button 0. You can do the following two operations:
- Press the button your finger is currently on. This will type out the character that is on the button.
- Move your finger to the other button. If your finger is on button
0, move it to button1, 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 of . More formally, you can choose two indices , and reverse the substring , 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 .
A string is a substring of a string if can be obtained from 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 (). The description of the test cases follows.
The first line of each test case contains a single integer () — the length of the binary string .
The second line of each test case contains a binary string ( or ) — the characters of the binary string .
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output the minimum cost of string 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 three times to type 000.
In the second test case, we can choose not to reverse any substrings. We can do operation to move our finger to button 1. Then, we do operation three times to type 111.
In the third test case, we can choose not to reverse any substring. We can do operation to type 0. Then, we do operation to move our finger to button 1. Finally, we do operation two times to type 11, resulting in the final string 011 using only operations.
In the fourth test case, we can reverse the substring , resulting in the string 001. We can do operation two times to type 00. Then we do operation to move our finger to button 1. Finally, we do operation once to type 1, resulting in the final string 001 using only operations.
In the fifth test case, we can reverse the substring , resulting in the string 11001. The cost of the string is as we can do the following sequence of operations:
- Do operation to move our finger to button
1. - Do operation two times to type
11. - Do operation to move our finger to button
0. - Do operation two times to type
00. - Do operation to move our finger to button
1. - Do operation once to type
1.
In the sixth test case, we can reverse the substring , resulting in the string 1101111011001001000. It can be proven that the minimum number of operations needed to type the binary string is .
Samples
6
3
000
3
111
3
011
3
100
5
10101
19
1101010010011011100
3
4
4
4
8
29
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |