Four problems for the stack: brackets, letters that cancel, a postfix calculator and a stack that knows its minimum.
- Check
empty()before everytop()andpop(), unless the statement promises the stack can't be empty there. - The strings go up to 2·10⁵ characters: one pass with a stack, never a search for each bracket's partner.
- For the letters that vanish in pairs, keep a
stack<char>: what is left on it at the end is the answer. Pour it into a second stack to print it in order. - In the postfix calculator, read every token as a
string:-5is a number, and only-on its own is an operator. Keeplong longon the stack. - Monotonic Stack is the stack that knows its minimum. Its time limit is tight, so keep the fast-IO lines.