#include <iostream> #include <bits/stdc++.h> #include <string.h> #include <vector> #include <utility> #include <iterator> #include <algorithm> #include <deque> #include <cmath> #define endl "\n" #define ll long long using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); string X ; cin>>X ; int a= 0 , b=0; for(int i=0;i<X.size();i++){//abbbaaaaaaaaaaaaaa if(X[i]=='a') a++ ; else b++ ; } int ans1=0; int Maxxi=INT_MIN ; for(int i=0 ;i<X.size() ;i++){ if(X[i]=='a'){ ans1++ ; Maxxi=max(ans1,Maxxi); } else { int a2=a-ans1; int ans2=ans1; for(int J=i ;J<X.size();J++){ if(X[J]=='b'){ ans2++; Maxxi=max(Maxxi,ans2+a2); } else a2--; } } } cout<<Maxxi; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter