CF1430D.String Deletion

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You have a string ss consisting of nn characters. Each character is either 0 or 1.

You can perform operations on the string. Each operation consists of two steps:

  1. select an integer ii from 11 to the length of the string ss, then delete the character sis_i (the string length gets reduced by 11, the indices of characters to the right of the deleted one also get reduced by 11);
  2. if the string ss is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix).

Note that both steps are mandatory in each operation, and their order cannot be changed.

For example, if you have a string s=s = 111010, the first operation can be one of the following:

  1. select i=1i = 1: we'll get 111010 →\rightarrow 11010 →\rightarrow 010;
  2. select i=2i = 2: we'll get 111010 →\rightarrow 11010 →\rightarrow 010;
  3. select i=3i = 3: we'll get 111010 →\rightarrow 11010 →\rightarrow 010;
  4. select i=4i = 4: we'll get 111010 →\rightarrow 11110 →\rightarrow 0;
  5. select i=5i = 5: we'll get 111010 →\rightarrow 11100 →\rightarrow 00;
  6. select i=6i = 6: we'll get 111010 →\rightarrow 11101 →\rightarrow 01.

You finish performing operations when the string ss becomes empty. What is the maximum number of operations you can perform?

你有一个由 nn 个字符组成的字符串 ss,每个字符均为 0 或 1。

你可以对字符串执行若干次操作。每次操作包含以下两个步骤:

  1. 选择一个整数 ii(取值范围为 11 到当前字符串 ss 的长度),然后删除字符 sis_i(字符串长度减 11,其右侧所有字符的下标也相应减 11);
  2. 若字符串 ss 非空,则删除最长的、由相同字符构成的前缀(剩余字符的下标及字符串长度均减少被删前缀的长度)。

注意:每次操作中,上述两个步骤均为强制执行,且顺序不可调换。

例如,若字符串 s=s = 111010,则第一次操作可能有如下几种情况:

  1. 选择 i=1i = 1:111010 →\rightarrow 11010 →\rightarrow 010;
  2. 选择 i=2i = 2:111010 →\rightarrow 11010 →\rightarrow 010;
  3. 选择 i=3i = 3:111010 →\rightarrow 11010 →\rightarrow 010;
  4. 选择 i=4i = 4:111010 →\rightarrow 11110 →\rightarrow 0;
  5. 选择 i=5i = 5:111010 →\rightarrow 11100 →\rightarrow 00;
  6. 选择 i=6i = 6:111010 →\rightarrow 11101 →\rightarrow 01。

当字符串 ss 变为空时,操作结束。问:最多能执行多少次操作?

输入格式

The first line contains a single integer tt (1≤t≤10001 \le t \le 1000) — the number of test cases.

The first line of each test case contains a single integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the length of the string ss.

The second line contains string ss of nn characters. Each character is either 0 or 1.

It's guaranteed that the total sum of nn over test cases doesn't exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤10001 \le t \le 1000)—— 测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)—— 字符串 ss 的长度。

每个测试用例的第二行包含一个长度为 nn 的字符串 ss,其中每个字符均为 0 或 1。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, print a single integer — the maximum number of operations you can perform.

对于每个测试用例,输出一个整数——你能执行的最大操作次数。

输入输出样例

  • 输入#1

    5
    6
    111010
    1
    0
    1
    1
    2
    11
    6
    101010

    输出#1

    3
    1
    1
    1
    3

说明/提示

In the first test case, you can, for example, select i=2i = 2 and get string 010 after the first operation. After that, you can select i=3i = 3 and get string 1. Finally, you can only select i=1i = 1 and get empty string.

在第一个测试用例中,例如,你可以选择 i=2i = 2,第一次操作后得到字符串 010。之后,你可以选择 i=3i = 3,得到字符串 1。最后,你只能选择 i=1i = 1,从而得到空字符串。

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

首页