#CF1791C. Prepend and Append
Prepend and Append
题目描述
Timur 最初有一个二进制字符串 (长度可能为 )。他进行了若干次(可能为零次)如下操作:
- 在字符串的一端添加一个 ,在另一端添加一个 。例如,从字符串 出发,你可以得到 $\color{red}{\texttt{0}}\texttt{1011}\color{red}{\texttt{1}}$ 或 $\color{red}{\texttt{1}}\texttt{1011}\color{red}{\texttt{0}}$。
现在给你 Timur 最终得到的字符串。请你求出他最初可能拥有的字符串的最短长度是多少。
二进制字符串是指仅由 或 组成的字符串(可以为空串)。
输入格式
输入的第一行包含一个整数 (),表示测试用例的数量。
每个测试用例的第一行包含一个整数 (),表示 Timur 最终字符串的长度。
每个测试用例的第二行包含一个长度为 的字符串 ,仅由字符 或 组成,表示最终的字符串。
输出格式
对于每个测试用例,输出一个非负整数,表示 Timur 最初可能拥有的字符串的最短长度。注意,Timur 最初的字符串可能为空,此时应输出 。
样例
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
样例说明
在第一个测试用例中,Timur 最短可能的初始字符串是 ,他进行了如下操作:$\texttt{0} \to \color{red}{\texttt{1}}\texttt{0}\color{red}{\texttt{0}}$。
在第二个测试用例中,Timur 最短可能的初始字符串是 ,他进行了如下操作:$\texttt{11} \to \color{red}{\texttt{0}}\texttt{11}\color{red}{\texttt{1}}$。
在第三个测试用例中,Timur 最短可能的初始字符串是 ,他没有进行任何操作。
在第四个测试用例中,Timur 最短可能的初始字符串是空串(记作 ),他进行了如下操作:$\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}}$。
在第五个测试用例中,Timur 最短可能的初始字符串是 ,他进行了如下操作:$\texttt{101} \to \color{red}{\texttt{0}}\texttt{101}\color{red}{\texttt{1}} \to \color{red}{\texttt{1}}\texttt{01011}\color{red}{\texttt{0}}$。
由 ChatGPT 4.1 翻译
来源
Codeforces 1791C,英文题名 Prepend and Append。