B. Swap and Delete
想到了用1和0的数量判断交换几次后再之后全部删除,
但是想到二重暴力会超时。
看了题解才发现可以用计数判断哪个多一点,多的全部删除
#include <iostream>
#include <string>
using namespace std;
void solve()
{
string s;
cin >> s;
int n = s.size();
int sum0 = 0, sum1 = 0;
for (auto i : s)
{
if (i == '0') sum0 ++;
else sum1 ++;
}
for (auto i : s)
{
if (i == '0')
{
if (sum1) sum1 --;
else break;
}
else
{
if (sum0) sum0 --;
else break;
}
}
cout << sum1 + sum0 << endl;
}
int main()
{
int t;
cin >> t;
while (t -- )
{
solve();
}
return 0;
}
标签:--,sum1,else,Swap,sum0,Delete
From: https://www.cnblogs.com/isomer/p/18151802