CF1791C.Prepend and Append

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Timur initially had a binary string†^{\dagger} ss (possibly of length 00). He performed the following operation several (possibly zero) times:

  • Add 0\texttt{0} to one end of the string and 1\texttt{1} to the other end of the string. For example, starting from the string 1011\texttt{1011}, you can obtain either 010111\color{red}{\texttt{0}}\texttt{1011}\color{red}{\texttt{1}} or 110110\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?

†^{\dagger} A binary string is a string (possibly the empty string) whose characters are either 0\texttt{0} or 1\texttt{1}.

蒂穆尔最初有一个二进制字符串†^{\dagger} ss(长度可能为 00)。他执行了以下操作若干次(可能为零次):

  • 在字符串的一端添加字符 0\texttt{0},在另一端添加字符 1\texttt{1}。例如,从字符串 1011\texttt{1011} 出发,可得到 010111\color{red}{\texttt{0}}\texttt{1011}\color{red}{\texttt{1}} 或 110110\color{red}{\texttt{1}}\texttt{1011}\color{red}{\texttt{0}}。

现给出蒂穆尔最终得到的字符串。请问:他最初可能拥有的最短字符串长度是多少?

†^{\dagger} 二进制字符串是指一个字符串(可能为空),其每个字符均为 0\texttt{0} 或 1\texttt{1}。

输入格式

The first line of the input contains an integer tt (1≤t≤1001 \leq t \leq 100) — the number of testcases.

The first line of each test case contains an integer nn (1≤n≤20001 \leq n \leq 2000) — the length of Timur's final string.

The second line of each test case contains a string ss of length nn consisting of characters 0\texttt{0} or 1\texttt{1}, denoting the final string.

输入的第一行包含一个整数 tt(1≤t≤1001 \leq t \leq 100)—— 表示测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤20001 \leq n \leq 2000)—— 表示 Timur 最终字符串的长度。

每个测试用例的第二行包含一个长度为 nn 的字符串 ss,由字符 0\texttt{0} 或 1\texttt{1} 组成,表示最终字符串。

输出格式

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 00.

对于每个测试用例,输出一个非负整数——Timur 原始字符串的最短可能长度。注意,Timur 的原始字符串可以为空,此时应输出 00。

输入输出样例

  • 输入#1

    9
    3
    100
    4
    0111
    5
    10101
    6
    101010
    7
    1010110
    1
    1
    2
    10
    2
    11
    10
    1011011010

    输出#1

    1
    2
    5
    0
    3
    1
    0
    2
    4

说明/提示

In the first test case, the shortest possible string Timur started with is 0\texttt{0}, and he performed the following operation: 0→100\texttt{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\texttt{11}, and he performed the following operation: 11→0111\texttt{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\texttt{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 ε\varepsilon), and he performed the following operations: ε→10→0101→101010\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\texttt{101}, and he performed the following operations: 101→01011→1010110\texttt{101} \to \color{red}{\texttt{0}}\texttt{101}\color{red}{\texttt{1}} \to \color{red}{\texttt{1}}\texttt{01011}\color{red}{\texttt{0}}.

在第一个测试用例中,Timur 起始的最短可能字符串是 0\texttt{0},他执行了如下操作:0→100\texttt{0} \to \color{red}{\texttt{1}}\texttt{0}\color{red}{\texttt{0}}。

在第二个测试用例中,Timur 起始的最短可能字符串是 11\texttt{11},他执行了如下操作:11→0111\texttt{11} \to \color{red}{\texttt{0}}\texttt{11}\color{red}{\texttt{1}}。

在第三个测试用例中,Timur 起始的最短可能字符串是 10101\texttt{10101},他未执行任何操作。

在第四个测试用例中,Timur 起始的最短可能字符串是空字符串(我们记为 ε\varepsilon),他执行了如下操作:ε→10→0101→101010\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 起始的最短可能字符串是 101\texttt{101},他执行了如下操作:101→01011→1010110\texttt{101} \to \color{red}{\texttt{0}}\texttt{101}\color{red}{\texttt{1}} \to \color{red}{\texttt{1}}\texttt{01011}\color{red}{\texttt{0}}。

输入解题思路,AI测评打分。不知道怎么写?

首页