A14783 | Minority
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a string $s$ , consisting only of characters '0' and '1'.
You have to choose a contiguous substring of $s$ and remove all occurrences of the character, which is a strict minority in it, from the substring.
That is, if the amount of '0's in the substring is strictly smaller than the amount of '1's, remove all occurrences of '0' from the substring. If the amount of '1's is strictly smaller than the amount of '0's, remove all occurrences of '1'. If the amounts are the same, do nothing.
You have to apply the operation exactly once. What is the maximum amount of characters that can be removed?
You have to choose a contiguous substring of $s$ and remove all occurrences of the character, which is a strict minority in it, from the substring.
That is, if the amount of '0's in the substring is strictly smaller than the amount of '1's, remove all occurrences of '0' from the substring. If the amount of '1's is strictly smaller than the amount of '0's, remove all occurrences of '1'. If the amounts are the same, do nothing.
You have to apply the operation exactly once. What is the maximum amount of characters that can be removed?
输入格式
The first line contains a single integer $t$ ( $1 \le t \le 10^4$ ) — the number of testcases.
The only line of each testcase contains a non-empty string $s$ , consisting only of characters '0' and '1'. The length of $s$ doesn't exceed $2 \cdot 10^5$ .
The total length of strings $s$ over all testcases doesn't exceed $2 \cdot 10^5$ .
The only line of each testcase contains a non-empty string $s$ , consisting only of characters '0' and '1'. The length of $s$ doesn't exceed $2 \cdot 10^5$ .
The total length of strings $s$ over all testcases doesn't exceed $2 \cdot 10^5$ .
输出格式
For each testcase, print a single integer — the maximum amount of characters that can be removed after applying the operation exactly once.
输入输出样例
输入 #1
4 01 1010101010111 00110001000 1
输出 #1
0 5 3 0
In the first testcase, you can choose substrings "0", "1" or "01". In "0" the amount of '0' is $1$ , the amount of '1' is $0$ . '1' is a strict minority, thus all occurrences of it are removed from the substring. However, since there were $0$ of them, nothing changes. Same for "1". And in "01" neither of '0' or '1' is a strict minority. Thus, nothing changes. So there is no way to remove any characters.
In the second testcase, you can choose substring "10101010101". It contains $5$ characters '0' and $6$ characters '1'. '0' is a strict minority. Thus, you can remove all its occurrences. There exist other substrings that produce the same answer.
In the third testcase, you can choose substring "011000100". It contains $6$ characters '0' and $3$ characters '1'. '1' is a strict minority. Thus, you can remove all its occurrences.
In the second testcase, you can choose substring "10101010101". It contains $5$ characters '0' and $6$ characters '1'. '0' is a strict minority. Thus, you can remove all its occurrences. There exist other substrings that produce the same answer.
In the third testcase, you can choose substring "011000100". It contains $6$ characters '0' and $3$ characters '1'. '1' is a strict minority. Thus, you can remove all its occurrences.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted