CF2241C.RemovevomeR

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a binary string ss consisting only of the characters 0\texttt{0} and 1\texttt{1}.

In one operation, you can do the following:

  • Choose a substring∗^{\text{∗}} of ss that is a palindrome†^{\text{†}} of length at least 22.
  • Delete exactly one character from this chosen substring.

The remaining parts of the string are then concatenated to form the new string ss.

Find the minimum possible length of the string ss that can be achieved after applying this operation any number of times (possibly zero).

∗^{\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.

†^{\text{†}}A string aa of length mm is said to be palindrome if ai=am+1−ia_i = a_{m + 1 - i} for all 1≤i≤m1 \le i \le m.

给你一个仅由字符 0 和 1 组成的二进制字符串 ss。

在一次操作中,你可以执行以下步骤:

  • 选择 ss 的一个子串∗^{\text{∗}},该子串是一个长度至少为 22 的回文串†^{\text{†}};
  • 从该选定的子串中恰好删除一个字符。

然后将字符串剩余部分拼接起来,形成新的字符串 ss。

求经过任意次(可能为零次)上述操作后,字符串 ss 可能达到的最小可能长度。

∗^{\text{∗}} 若字符串 aa 可通过从字符串 bb 的开头删除若干(可能为零或全部)字符、并从结尾删除若干(可能为零或全部)字符而得到,则称 aa 是 bb 的一个子串。

†^{\text{†}} 设字符串 aa 的长度为 mm,若对所有 1≤i≤m1 \le i \le m 均有 ai=am+1−ia_i = a_{m + 1 - i},则称 aa 是一个回文串。

输入格式

The first line contains a single integer tt (1≤t≤1001 \le t \le 100) — the number of test cases. Description of each test case follows.

The first line of each test case contains a single integer nn (1≤n≤1001 \le n \le 100) — the length of the binary string ss.

The second line of each test case contains a binary string ss of length nn. It is guaranteed that each character of ss is either 0\texttt{0} or 1\texttt{1}.

第一行包含一个整数 tt(1≤t≤1001 \le t \le 100)—— 表示测试用例的数量。随后是每个测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤1001 \le n \le 100)—— 表示二进制字符串 ss 的长度。

每个测试用例的第二行包含一个长度为 nn 的二进制字符串 ss。保证 ss 的每个字符均为 0\texttt{0} 或 1\texttt{1}。

输出格式

For each test case, print the minimum possible length of the string ss that can be achieved after applying the operation any number of times.

对于每个测试用例,输出在任意次数执行该操作后字符串 ss 可能达到的最小长度。

输入输出样例

  • 输入#1

    4
    4
    0000
    3
    110
    6
    110011
    6
    101100

    输出#1

    1
    2
    1
    1

说明/提示

In the first test case, the initial string is 0000\texttt{0000}. We can perform the following sequence of operations:

  • Choose the palindromic substring 0000\texttt{0000}. Delete one 0\texttt{0}. The string becomes 000\texttt{000}.
  • Choose the palindromic substring 000\texttt{000}. Delete one 0\texttt{0}. The string becomes 00\texttt{00}.
  • Choose the palindromic substring 00\texttt{00}. Delete one 0\texttt{0}. The string becomes 0\texttt{0}.

The string 0\texttt{0} contains no palindromic substrings of length at least 22, so no further operations can be performed. The minimum possible length is 11.

In the second test case, the initial string is 110\texttt{110}.

  • Choose the palindromic substring 11\texttt{11}. Delete one 1\texttt{1}. The string becomes 10\texttt{10}.

The string 10\texttt{10} contains no palindromic substrings of length at least 22, so no further operations can be performed. The minimum possible length is 22.

在第一个测试用例中,初始字符串为 0000\texttt{0000}。我们可以执行以下操作序列:

  • 选择回文子串 0000\texttt{0000},删除其中一个 0\texttt{0},字符串变为 000\texttt{000}。
  • 选择回文子串 000\texttt{000},删除其中一个 0\texttt{0},字符串变为 00\texttt{00}。
  • 选择回文子串 00\texttt{00},删除其中一个 0\texttt{0},字符串变为 0\texttt{0}。

字符串 0\texttt{0} 不包含长度至少为 22 的回文子串,因此无法再执行任何操作。最小可能长度为 11。

在第二个测试用例中,初始字符串为 110\texttt{110}。

  • 选择回文子串 11\texttt{11},删除其中一个 1\texttt{1},字符串变为 10\texttt{10}。

字符串 10\texttt{10} 不包含长度至少为 22 的回文子串,因此无法再执行任何操作。最小可能长度为 22。

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

首页