欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1791C.Prepend and Append
Prepend and Append
Timur initially had a binary string (possibly of length ). He performed the following operation several (possibly zero) times:
- Add
0to one end of the string and1to the other end of the string. For example, starting from the string1011, you can obtain either $\color{red}{\texttt{0}}\texttt{1011}\color{red}{\texttt{1}}$ or $\color{red}{\texttt{1}}\texttt{1011}\color{red}{\texttt{0}}$.
You are given Timur's final string. What is the length of the shortest possible string he could have started with?
A binary string is a string (possibly the empty string) whose characters are either 0 or 1.
Input
The first line of the input contains an integer () — the number of testcases.
The first line of each test case contains an integer () — the length of Timur's final string.
The second line of each test case contains a string of length consisting of characters 0 or 1, denoting the final string.
Output
For each test case, output a single nonnegative integer — the shortest possible length of Timur's original string. Note that Timur's original string could have been empty, in which case you should output .
Note
In the first test case, the shortest possible string Timur started with is 0, and he performed the following operation: 0} \to \color{red}{\texttt{1}}\texttt{0}\color{red}{\texttt{0}.
In the second test case, the shortest possible string Timur started with is 11, and he performed the following operation: 11} \to \color{red}{\texttt{0}}\texttt{11}\color{red}{\texttt{1}.
In the third test case, the shortest possible string Timur started with is 10101, and he didn't perform any operations.
In the fourth test case, the shortest possible string Timur started with is the empty string (which we denote by ), and he performed the following operations: $\varepsilon \to \color{red}{\texttt{1}}\texttt{}\color{red}{\texttt{0}} \to \color{red}{\texttt{0}}\texttt{10}\color{red}{\texttt{1}} \to \color{red}{\texttt{1}}\texttt{0101}\color{red}{\texttt{0}}$.
In the fifth test case, the shortest possible string Timur started with is 101, and he performed the following operations: 101} \to \color{red}{\texttt{0}}\texttt{101}\color{red}{\texttt{1}} \to \color{red}{\texttt{1}}\texttt{01011}\color{red}{\texttt{0}.
Samples
9
3
100
4
0111
5
10101
6
101010
7
1010110
1
1
2
10
2
11
10
1011011010
1
2
5
0
3
1
0
2
4
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |