fork download
  1. /*
  2. Task: 877B
  3. Date: Dec 21, 2020
  4. Author: aLittleLove (Minh Vu)
  5. */
  6.  
  7. #include<bits/stdc++.h>
  8. #define rep(i,n) for (int i=0, _n=n; i<_n; i++)
  9. #define FOR(i,a,b) for (int _a=(a), _b=(b), i=_a; _a<=_b?i<=_b:i>=_b; _a<=_b?i++:i--)
  10. #define _mem(a, b) memset(a, (b), sizeof(a))
  11. #define pb push_back
  12. #define fi first
  13. #define se second
  14. #define sz(a) int((a).size())
  15.  
  16. using namespace std;
  17. typedef long long ll;
  18. typedef int64_t i64;
  19. typedef pair<int, int> pii;
  20. typedef vector<pii> vii;
  21. typedef vector<int> vi;
  22. const int N = 2e5 + 5;
  23. const int inf = 1e9;
  24. const int mod = 1e9 + 7;
  25. const double pi = atan(1) * 4.0;
  26. template<typename T, typename U> inline void mini(T &x, U y) { if(y < x) x = y; }
  27. template<typename T, typename U> inline void maxi(T &x, U y) { if(x < y) x = y; }
  28.  
  29. int prefA[N], prefB[N];
  30.  
  31. void Solve()
  32. {
  33. string s; cin >> s;
  34. int n = sz(s);
  35. s = ' ' + s;
  36. for (int i=1; i<=n; i++)
  37. {
  38. prefA[i] = prefA[i - 1] + (s[i]=='a');
  39. prefB[i] = prefB[i - 1] + (s[i]=='b');
  40. }
  41. int res = 0;
  42. for (int i=1; i<=n; i++)
  43. {
  44. for (int j=i; j<=n; j++)
  45. {
  46. res = max(res, prefA[i] + prefB[j] - prefB[i] + prefA[n] - prefA[j]);
  47. res = max(res, prefB[j] + prefA[n] - prefA[j]);
  48. res = max(res, prefA[i] + prefB[n] - prefB[i]);
  49. res = max(res, prefB[j]);
  50. res = max(res, prefA[j]);
  51. }
  52. }
  53. cout << res << '\n';
  54. }
  55.  
  56. int main()
  57. {
  58. ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  59. //freopen("input.txt","r",stdin);
  60. int nTest; //cin >> nTest;
  61. nTest = 1;
  62. while (nTest--) Solve();
  63.  
  64. return 0;
  65. }
Success #stdin #stdout 0s 4956KB
stdin
bbabbbababaa
stdout
9