教育守则第122轮B题

        codefores,问我为啥只更新A,B题,因为只会这两个,好尴尬啊,但是本菜菜也致力于把这两道题写好,写成最美的代码!!!

上题目:

B. Minority

        You are given a string ss, consisting only of characters '0' and '1'.

        You have to choose a contiguous substring of ss 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 tt (1≤t≤1041≤t≤104) — the number of testcases.

The only line of each testcase contains a non-empty string ss, consisting only of characters '0' and '1'. The length of ss doesn't exceed 2⋅1052⋅105.

The total length of strings ss over all testcases doesn't exceed 2⋅1052⋅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 11, the amount of '1' is 00. '1' is a strict minority, thus all occurrences of it are removed from the substring. However, since there were 00 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 55 characters '0' and 66 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 66 characters '0' and 33 characters '1'. '1' is a strict minority. Thus, you can remove all its occurrences.

B. 少数

        您将获得一个字符串ss,仅由字符"0"和"1"组成。

        您必须选择连续的子字符串ss并从子字符串中删除字符的所有匹配项,这是其中的严格少数。也就是说,如果子字符串中"0"的数量严格小于"1"的数量,请从子字符串中删除所有出现的"0"。如果"1"的数量严格小于"0"的数量,请删除所有出现的"1"。如果金额相同,则不执行任何操作。

        您必须只应用一该操作。可以删除的最大字符数是多少?

输入

第一行包含单个整数t(1≤t≤10^4) ― 测试用例的数量。

每个测试用例的唯一行包含一个非空字符串ss,仅由字符"0"和"1"组成。长度s不超过2⋅10^5

字符串的总长度ss在所有测试用例中不超过2⋅10^5

输出

对于每个测试用例,打印一个整数 — 在应用操作一次后可以删除的最大字符数.

输入
4
01
1010101010111
00110001000
1
输出
0
5
3
0
注意:

        在第一个测试用例中,可以选择子字符串"0"、"1"或"01"。在"0"中,"0"的量为11,"1"的量为00."1"是严格的少数,因此它的所有出现都将从子字符串中删除。但是,由于有00其中,没有任何变化。与"1"相同。在"01"中,"0"或"1"都不是严格的少数。因此,没有任何变化。因此,无法删除任何字符。

        在第二个测试用例中,您可以选择子字符串"10101010101"。它包含55字符"0"和66字符"1"。"0"是严格的少数。因此,您可以删除其所有匹配项。还有其他子字符串产生相同的答案。

        在第三个测试用例中,您可以选择子字符串"011000100"。它包含66字符"0"和33字符"1"。"1"是严格的少数。因此,您可以删除其所有匹配项。

 开始分析:

        这道题咋说,是一道你会就非常简单的题目,主要就是总结规律,分别计算0,1的个数(千万不要被子序列干扰,因为它要求出的是删除的最大个数,不注意这个点真的耽误好久好久),如果0大就输出1的个数,如果1的个数大就输出0,如果相等就个数减少一个输出(是不是很简单!!!)

#include <iostream>
using namespace std;

int main()
{
    int t;   
    string s;
    cin >> t;
    while (t--) {
        int cnt1 = 0;
        int cnt2 = 0;
        cin >> s;
        if (s.size() == 1) {
            cout << 0 << endl;
            continue;
        }
        for (int i = 0; i < s.size(); i++) {
            if (s[i] == '0') {
                cnt1++;
            }
            if(s[i]=='1') {
                cnt2++;
            }
        }
        if (cnt1 > cnt2) {
            cout << cnt2<< endl;
            continue;
        }
        else if(cnt1<cnt2) {
            cout << cnt1 << endl;
            continue;
        }
        else if (cnt1 == cnt2) {
            cout << cnt1 - 1 << endl;
            continue;
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

钟一淼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值