1873G - ABBC or BACB - CodeForces Solution


greedy

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
int main(void)
{
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int a,b,c,d,e,f,g,h,j;
	string str,str2;
	cin>>a;
	while(a--)
	{
		cin>>str;
		b=str.size();
		d=0;//ans
		e=2e9;
		f=0;
		bool fl=false;
		for(c=0;c<b;c++)
		{
			if(str[c]=='A')
			{
				++d;
				++f;
			}
			else
			{
				if(c!=0 && g=='B')
					fl=true;
				if(g!='B')
				{
					e=min(e,f);
					f=0;
				}
			}
			g=str[c];
		}
		if(f!=0)
			e=min(e,f);
		if(str[0]=='B' || str[b-1]=='B')
			fl=true;
		if(fl)
			cout<<d<<"\n";
		else
			cout<<d-e<<"\n";
	}

	return 0;
}


Comments

Submit
0 Comments
More Questions

766A - Mahmoud and Longest Uncommon Subsequence
701B - Cells Not Under Attack
702A - Maximum Increase
1656D - K-good
1426A - Floor Number
876A - Trip For Meal
1326B - Maximums
1635C - Differential Sorting
961A - Tetris
1635B - Avoid Local Maximums
20A - BerOS file system
1637A - Sorting Parts
509A - Maximum in Table
1647C - Madoka and Childish Pranks
689B - Mike and Shortcuts
379B - New Year Present
1498A - GCD Sum
1277C - As Simple as One and Two
1301A - Three Strings
460A - Vasya and Socks
1624C - Division by Two and Permutation
1288A - Deadline
1617A - Forbidden Subsequence
914A - Perfect Squares
873D - Merge Sort
1251A - Broken Keyboard
463B - Caisa and Pylons
584A - Olesya and Rodion
799A - Carrot Cakes
1569B - Chess Tournament