CF1971D.Binary Cut

传统题 时间 2000 ms 内存 256 MiB 5 尝试 1 已通过 1 标签

Binary Cut

You are given a binary string^{\dagger}. Please find the minimum number of pieces you need to cut it into, so that the resulting pieces can be rearranged into a sorted binary string.

Note that:

  • each character must lie in exactly one of the pieces;
  • the pieces must be contiguous substrings of the original string;
  • you must use all the pieces in the rearrangement.

^{\dagger} A binary string is a string consisting of characters 0 and 1. A sorted binary string is a binary string such that all characters 0 come before all characters 1.

Input

The first line contains a single integer tt (1t5001 \leq t \leq 500) — the number of test cases.

The only line of each test case contains a single string ss (1s5001 \leq |s| \leq 500) consisting of characters 0 and 1, where s|s| denotes the length of the string ss.

Output

For each test case, output a single integer — the minimum number of pieces needed to be able to rearrange the string into a sorted binary string.

Note

The first test case is pictured in the statement. It can be proven that you can't use fewer than 33 pieces.

In the second and third test cases, the binary string is already sorted, so only 11 piece is needed.

In the fourth test case, you need to make a single cut between the two characters and rearrange them to make the string 01.

Samples

6
11010
00000000
1
10
0001111
0110
3
1
1
2
1
2

在线编程 IDE

建议全屏模式获得最佳体验