Skip to content

Instantly share code, notes, and snippets.

@lundstig
Last active March 12, 2019 14:10
Show Gist options
  • Save lundstig/3babade2516989ccdac2ffa37234cb14 to your computer and use it in GitHub Desktop.
Save lundstig/3babade2516989ccdac2ffa37234cb14 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
#define rep(x, s, e) for (int x = int(s); x < int(e); ++x)
using namespace std;
int main() {
string s;
cin >> s;
// Create prefix arrays counting a's and b's *before* position
vector<int> a(s.size() + 1);
vector<int> b(s.size() + 1);
a[0] = b[0] = 0;
rep(i, 0, s.size()) {
a[i + 1] = a[i] + (s[i] == 'a'); // true is converted to 1, false to 0
b[i + 1] = b[i] + (s[i] == 'b');
}
int ret = 0;
rep(i, 0, s.size() + 1) { // Letter after first part
rep(j, i, s.size() + 1) { // Letter after middle part
int first = a[i];
int middle = b[j] - b[i];
int last = a.back() - a[j];
ret = max(ret, first + middle + last);
}
}
cout << ret << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment