CF 1633B - Minority
2023-06-21 18:54:38 来源: 哔哩哔哩
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?
Input
The first line contains a single integer t (1≤t≤104) — 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⋅105.
The total length of strings s over all testcases doesn't exceed 2⋅105.
Output
For each testcase, print a single integer — the maximum amount of characters that can be removed after applying the operation exactly once.
Example
input
4
01
1010101010111
00110001000
1
output
0
5
3
0
Note
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.
放2个参数依次记录当前0和1的数量,每次去比较两个数谁小,保存,依次遍历完即可,但是要在两个数不相等的情况下,这里踩了一个坑。
标签:
[责任编辑:]
猜你喜欢
- (2023-06-21)漫威英雄自画像,幻视的画工糟糕,冬兵这算是在告白? 天天快报
- (2023-06-21)当前快讯:2023年中国消防救援学院在北京市招收青年学生考核测试工作的通知
- (2023-06-21)鸿利智汇:公司COB订单有所增长,目前上述产品尚未进入手机产业链|环球视点
- (2023-06-21)年轻人嫌它老气、老年人嫌它怪异,丰田皇冠的指导价还重要吗?
- (2023-06-21)环球热点评!马来西亚Berjaya航空购买两架高端ATR72-600
- (2023-06-21)今日讯!天富龙IPO:原材料涨价致毛利率逐年下滑 主要产品销量下滑拦不住扩产脚步
- (2023-06-21)全球资讯:河北武强手艺人400多道工序制作单簧管 20多年制作7000余支