#include using namespace std; unsigned int swapAB( istream& in ) { const auto MAX_LENGTH( 12u ); char buffer[ MAX_LENGTH + 1 ]; in >> buffer; auto s = string( buffer ); auto numberOfMove( 0ul ), indexA( s.length() - 1ul ), indexB( 0ul ); auto isFirstTime( true ); while ( 1 ) { if ( ( indexA = s.rfind( 'A', isFirstTime ? indexA : indexA - 1 ) ) == string::npos ) break; if ( ( indexB = s.find( 'B', isFirstTime ? indexB : indexB + 1 ) ) == string::npos ) break; if ( indexB < indexA ) numberOfMove += indexA - indexB; isFirstTime = false; } return numberOfMove; } int main() { cout << swapAB( cin ) << endl; return 0; }