MIN-MEX Cut solution codeforces- A binary string is a string that consists of characters 00 and 11. Let MEXMEX of a binary string be the smallest digit among 00, 11, or 22 that does not occur in the string. For example, MEXMEX of 001011001011 is 22, because 00 and 11 occur in the string at least once, MEXMEX of 11111111 is 00, because 00 and 22 do not occur in the string and 0<20<2.

MIN-MEX Cut solution codeforces

A binary string is a string that consists of characters 00 and 11.

Let MEXMEX of a binary string be the smallest digit among 0011, or 22 that does not occur in the string. For example, MEXMEX of 001011001011 is 22, because 00 and 11 occur in the string at least once, MEXMEX of 11111111 is 00, because 00 and 22 do not occur in the string and 0<20<2.

A binary string ss is given. You should cut it into any number of substrings such that each character is in exactly one substring. It is possible to cut the string into a single substring — the whole string.

A string aa is a substring of a string bb if aa can be obtained from bb by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

What is the minimal sum of MEXMEX of all substrings pieces can be?

MIN-MEX Cut solution codeforces

The input consists of multiple test cases. The first line contains a single integer tt (1t1041≤t≤104) — the number of test cases. Description of the test cases follows.

Each test case contains a single binary string ss (1|s|1051≤|s|≤105).

It’s guaranteed that the sum of lengths of ss over all test cases does not exceed 105105.

MIN-MEX Cut solution codeforces

For each test case print a single integer — the minimal sum of MEXMEX of all substrings that it is possible to get by cutting ss optimally.

Example

input

Copy
6
01
1111
01100
101
0000
01010

output

MIN-MEX Cut solution codeforces
1
0
2
1
1
2
Note

In the first test case the minimal sum is MEX(0)+MEX(1)=1+0=1MEX⁡(0)+MEX⁡(1)=1+0=1.

In the second test case the minimal sum is MEX(1111)=0MEX⁡(1111)=0.

In the third test case the minimal sum is MEX(01100)=2MEX⁡(01100)=2.

Leave a Comment

Your email address will not be published.