#P14015. [ICPC 2024 Nanjing R] 生日礼物

    ID: 13987 远端评测题 1000ms 1024MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划 DP贪心2024ICPCAd-hoc南京

[ICPC 2024 Nanjing R] 生日礼物

Description

Grammy's birthday is approaching, and she gets a sequence AA from her friends as a gift. The sequence consists of only 00, 11, and 22. Grammy thinks that the sequence is too long, so she decides to modify AA to make it shorter.

Formally, Grammy can perform an arbitrary number of operations. Each time she can choose one of the following three operations to perform:

  • Change any 22 into 00 or 11.
  • Choose two adjacent 00s, erase them, and concatenate the rest of the parts.
  • Choose two adjacent 11s, erase them, and concatenate the rest of the parts.

Calculate the minimum sequence length Grammy can get.

Input Format

There are multiple test cases. The first line of the input contains an integer TT indicating the number of test cases. For each test case:

The first and only line contains a string of length nn (1n2×1051\leq n\leq 2 \times 10^5) consisting of digits 00, 11, and 22, indicating the initial sequence AA.

It is guaranteed that the sum of nn of all test cases will not exceed 5×1055 \times 10^5.

Output Format

For each test case, output one line containing one integer indicating the minimum sequence length Grammy can get.

5
0110101
01020102
0000021111
1012121010
0100202010
3
4
0
6
0