EMTY3 - Can You Make It Empty 3

Let us introduce an algorithm with a function CanEmpty() which takes a string P as a parameter and return TRUE if it is possible to make P empty, otherwise return FALSE.

String P consists of only 0 and 1. The pseudo-code implementation of CanEmpty() function is as follows.

bool CanEmpty(String P)
{
     while (P has at least one substring 100)
     {
         Chose any one substring 100 in P and delete it.
     }
     if (P is empty) return TRUE;
     else return FALSE;
}

Now you are given a string S consisting of 0 and 1, you have to find the length of longest substring of S that can be made empty applying CanEmpty() algorithm.

As for example, let S=1011100000100

S has only two sub-strings (bold) which can be made empty applying CanEmpty() algorithm.

The first substring will have the delete- sequence in CanEmpty() function :

110000->100->empty

The second substring will have the delete-sequence in CanEmpty() function:

100->empty

The length of first substring is 6 and second is 3. So, the required answer is 6.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains a string S. The size of string is at most 200000.

Output

For each test case, print the case number and required answer.

Sample Input

Output for Sample Input

2
1011100000100
111011

Case 1: 6
Case 2: 0

Problem Setter: Md Abdul Alim, Dept. of Computer Science, Bangladesh University of Business & Technology


Added by:Alim
Date:2014-10-08
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: GOSU
Resource:Own Problem

hide comments
2024-02-17 07:11:06 Sushovan Sen
@nightwolf_9197
Correct answer is 33
2019-03-25 08:29:56
@Md Abdul Alim can you please tell me where my code goes wrong as it passes on all of my test cases.
100111000010010010011110000000000000000
my output is 27 for this one
2016-07-31 12:01:50
0.09 without fast io
2015-08-27 20:13:11
Nice problem :)
O(N) 0.05 as well :D
2015-07-08 23:56:00 :.Mohib.:
@lakshman I also.... did it in O(N)..0.05s :)
EDIT: 0.04s now... ;)
Strong test cases...very Nice problem.... :)

Last edit: 2015-07-09 00:10:04
2015-06-28 17:51:28 [Lakshman]
@Mohib I did this in O(N).
2015-06-26 13:04:45 :.Mohib.:
What complexity expected???
2014-12-30 12:09:07 Diksha Jaiswal
@ivar.raknahs it is 15.


Last edit: 2015-06-17 07:23:39
2014-12-21 12:15:39 ivar.raknahs
@Md Abdul Alim: what should be the o/p of 100100111000000

9 or 15
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.