A. Love "A"
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Alice has a string ss. She really likes the letter "a". She calls a string good if strictly more than half of the characters in that string are "a"s. For example "aaabb", "axaa" are good strings, and "baca", "awwwa", "" (empty string) are not.
Alice can erase some characters from her string ss. She would like to know what is the longest string remaining after erasing some characters (possibly zero) to get a good string. It is guaranteed that the string has at least one "a" in it, so the answer always exists.
Input
The first line contains a string ss (1≤|s|≤501≤|s|≤50) consisting of lowercase English letters. It is guaranteed that there is at least one "a" in ss.
Output
Print a single integer, the length of the longest good string that Alice can get after erasing some characters from ss.
Note
In the first example, it's enough to erase any four of the "x"s. The answer is 33 since that is the maximum number of characters that can remain.
In the second example, we don't need to erase any characters.
水题,只要a的数量比字符串的其余数量少,就可以输出总数,反之只要输出二倍的a再减去1就好了
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <string.h>
#include <string>
#include <cctype>
typedef long long ll;
#define INF 0x3f3f3f3f
#define mem memset
#define sc scanf
#define pr printf
using namespace std;
//inline int read(){
// int X = 0, w = 0; char ch = 0;
// while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
// while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
// return w ? -X : X;
//}
//inline double dbread(){
// double X = 0, Y = 1.0; int w = 0; char ch = 0;
// while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
// while (isdigit(ch)) X = X * 10 + (ch ^ 48), ch = getchar();
// ch = getchar();//读入小数点
// while (isdigit(ch)) X += (Y /= 10) * (ch ^ 48), ch = getchar();
// return w ? -X : X;
//}
//inline void write(int x){
// if (x < 0) putchar('-'), x = -x;
// if (x > 9) write(x / 10);
// putchar(x % 10 + '0');
//}
int main() {
ios::sync_with_stdio(false);
string s;
cin >> s;
int len = s.length();
int num1 = 0, num2 = 0;
for (int i = 0; i < len; ++i) {
if (s[i] == 'a') num1++;
else num2++;
}
if (num1 > num2) cout << num1 + num2 << endl;
else cout << num1 * 2 - 1 << endl;
return 0;
}