A13783 | String Deletion
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You have a string $s$ consisting of $n$ 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 $i$ from $1$ to the length of the string $s$ , then delete the character $s_i$ (the string length gets reduced by $1$ , the indices of characters to the right of the deleted one also get reduced by $1$ );
2. if the string $s$ 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 =$ 111010, the first operation can be one of the following:
1. select $i = 1$ : we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010;
2. select $i = 2$ : we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010;
3. select $i = 3$ : we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010;
4. select $i = 4$ : we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0;
5. select $i = 5$ : we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00;
6. select $i = 6$ : we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01.
You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform?
You can perform operations on the string. Each operation consists of two steps:
1. select an integer $i$ from $1$ to the length of the string $s$ , then delete the character $s_i$ (the string length gets reduced by $1$ , the indices of characters to the right of the deleted one also get reduced by $1$ );
2. if the string $s$ 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 =$ 111010, the first operation can be one of the following:
1. select $i = 1$ : we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010;
2. select $i = 2$ : we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010;
3. select $i = 3$ : we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010;
4. select $i = 4$ : we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0;
5. select $i = 5$ : we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00;
6. select $i = 6$ : we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01.
You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform?
输入格式
The first line contains a single integer $t$ ( $1 \le t \le 1000$ ) — the number of test cases.
The first line of each test case contains a single integer $n$ ( $1 \le n \le 2 \cdot 10^5$ ) — the length of the string $s$ .
The second line contains string $s$ of $n$ characters. Each character is either 0 or 1.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$ .
The first line of each test case contains a single integer $n$ ( $1 \le n \le 2 \cdot 10^5$ ) — the length of the string $s$ .
The second line contains string $s$ of $n$ characters. Each character is either 0 or 1.
It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \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 = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted