#GD0000201. Xóa các cặp AB và BB (ABBB)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Xóa các cặp AB và BB (ABBB)
ABBB
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
A zookeeper has a string consisting only of A and B. One operation removes two adjacent characters if they form either AB or BB.
After deletion, the remaining parts are concatenated, possibly creating new removable pairs. You may perform the operations in any order, stopping when appropriate.
Find the minimum possible length of the remaining string after a sequence of valid operations. Do not print the resulting string itself.
Input
The only line contains a nonempty string consisting solely of A and B.
Output
Print one integer: the minimum possible number of characters remaining after valid AB or BB deletions.
Subtasks
- Subtask 1 (20 points): ; Every character of is
AorB. - Subtask 2 (30 points): ; Every character of is
AorB. - Subtask 3 (50 points): ; Every character of is
AorB.
Examples
Input
BABA
Output
2
Explanation
Start with BABA. Deleting the middle AB produces BA, length . No legal pair remains. An empty result would require two deletions, but the leading B cannot be removed using the trailing A. Thus the minimum length is .