A15146 | Binary String
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a string $s$ consisting of characters 0 and/or 1.
You have to remove several (possibly zero) characters from the beginning of the string, and then several (possibly zero) characters from the end of the string. The string may become empty after the removals. The cost of the removal is the maximum of the following two values:
- the number of characters 0 left in the string;
- the number of characters 1 removed from the string.
What is the minimum cost of removal you can achieve?
You have to remove several (possibly zero) characters from the beginning of the string, and then several (possibly zero) characters from the end of the string. The string may become empty after the removals. The cost of the removal is the maximum of the following two values:
- the number of characters 0 left in the string;
- the number of characters 1 removed from the string.
What is the minimum cost of removal you can achieve?
输入格式
The first line contains one integer $t$ ( $1 \le t \le 10^4$ ) — the number of test cases.
Each test case consists of one line containing the string $s$ ( $1 \le |s| \le 2 \cdot 10^5$ ), consisting of characters 0 and/or 1.
The total length of strings $s$ in all test cases does not exceed $2 \cdot 10^5$ .
Each test case consists of one line containing the string $s$ ( $1 \le |s| \le 2 \cdot 10^5$ ), consisting of characters 0 and/or 1.
The total length of strings $s$ in all test cases does not exceed $2 \cdot 10^5$ .
输出格式
For each test case, print one integer — the minimum cost of removal you can achieve.
输入输出样例
输入 #1
5 101110110 1001001001001 0000111111 00000 1111
输出 #1
1 3 0 0 0
Consider the test cases of the example:
1. in the first test case, it's possible to remove two characters from the beginning and one character from the end. Only one 1 is deleted, only one 0 remains, so the cost is $1$ ;
2. in the second test case, it's possible to remove three characters from the beginning and six characters from the end. Two characters 0 remain, three characters 1 are deleted, so the cost is $3$ ;
3. in the third test case, it's optimal to remove four characters from the beginning;
4. in the fourth test case, it's optimal to remove the whole string;
5. in the fifth test case, it's optimal to leave the string as it is.
1. in the first test case, it's possible to remove two characters from the beginning and one character from the end. Only one 1 is deleted, only one 0 remains, so the cost is $1$ ;
2. in the second test case, it's possible to remove three characters from the beginning and six characters from the end. Two characters 0 remain, three characters 1 are deleted, so the cost is $3$ ;
3. in the third test case, it's optimal to remove four characters from the beginning;
4. in the fourth test case, it's optimal to remove the whole string;
5. in the fifth test case, it's optimal to leave the string as it is.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted