main = getLine>>=print.solve

solve :: String -> Int
solve s = sum $ map f converted
  where
  converted = words $ go s
    where
    go [] = []
    go ('A':s) = 'a' : go s
    go ('B':'C':s) = 'b' : go s
    go (c:s) = ' ' : go s
  count a = length.filter(==a)
  f s = go (count 'b' s) s
  go _ [] = 0
  go n ('a':s) = n + go n s
  go n ('b':s) = go (n-1) s
  go _ _ = undefined