Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 months ago
795 B
0
Indexable
Never
#include <algorithm>
#include <iostream>
#include <vector>

int main()
{
  std::string s;
  std::cin >> s;
  auto cnt_a = std::count(s.begin(), s.end(), 'A');
  auto cnt_b = std::count(s.begin(), s.end(), 'B');
  int cnt = 0;
  // 先填充直到相等
  if (cnt_a != cnt_b)
  {
    if (cnt_a > cnt_b)
    {

      for (int i = 0; i < cnt_a - cnt_b; ++i)
      {
        s += 'B';
        cnt++;
      }
    }
    else
    {
      for (int i = 0; i < cnt_b - cnt_a; ++i)
      {
        s = 'A' + s;
        cnt++;
      }
    }
  }
  int vote = 0;
  for (auto &i : s)
  {
    if (i == 'B' && vote < 0)
    {
      vote++;
    }
    if (i == 'A')
    {
      vote--;
    }
  }
  vote = vote > 0 ? 0 : -vote;
  std::cout << cnt + vote << std::endl;
}
Leave a Comment