#![allow(unused_imports)] use std::io::{ self, Write }; use std::str::FromStr; use std::cmp::{ min, max }; use std::collections::{ BinaryHeap, VecDeque, HashMap }; #[allow(unused_macros)] macro_rules! trace { ($var:expr) => { let _ = writeln!(&mut std::io::stderr(), ">>> {} = {:?}", stringify!($var), $var); }; ($($args:expr),*) => { trace!(($($args),*)) } } #[allow(unused_macros)] macro_rules! put { ($var:expr) => { let _ = writeln!(&mut std::io::stdout(), "{}", $var); }; ($var:expr, $($args:expr),*) => { let _ = write!(&mut std::io::stdout(), "{} ", $var); put!($($args),*); }; } const M: usize = 1_000_000_007; fn main() { let mut sc = Scanner::new(); let mut ans = 0; let mut memo = HashMap::new(); let mut ac = 0; let mut idx = 1; memo.insert(0, 0); for c in sc.cin::().chars() { if c == 'A' { ac += 1; } else { ac -= 1; } if memo.contains_key(&ac) { ans = max(ans, idx - memo[&ac]); } else { memo.insert(ac, idx); } idx += 1; } put!(ans); } #[allow(dead_code)] struct Scanner { stdin: io::Stdin, buffer: VecDeque, } #[allow(dead_code)] impl Scanner { fn new() -> Scanner { Scanner { stdin: io::stdin(), buffer: VecDeque::new() } } fn reserve(&mut self) { while self.buffer.len() == 0 { let mut line = String::new(); let _ = self.stdin.read_line(&mut line); for w in line.split_whitespace() { self.buffer.push_back(String::from(w)); } } } fn cin(&mut self) -> T { self.reserve(); match self.buffer.pop_front().unwrap().parse::() { Ok(a) => a, Err(_) => panic!("parse err") } } }