DYZIO - Dyzio

Dyzio is Jasiek's friend and he also likes riddles. Here is a riddle he came up with:

Jasiek, here is a piece of rope, which has to be cut into smaller pieces. I will not tell you directly how to do it, but look at this sequence of zeros (0) and ones (1). A one at the beginning means that the rope has to be cut in half. If the first digit was zero, it would be the only digit in the sequence and mean you don't have to cut anything - I want the whole rope. If you have to cut the rope anyway then after the first 1 I wrote what to do with the left piece (according to the same rules as with the whole rope) and then I wrote what to do with the right piece of rope (all the time with the same rules of notation). Every time you have to cut the left piece first, only then can you cut the right one. Now start cutting and tell me, how many cuts you have to do until you have cut off the shortest piece.

Unfortunately mom hid the scissors from Jasiek, but luckily a computer was at hand and Jasiek quickly wrote a program simulating the rope cutting. Can you write such a program?

Task

Write a program which

  • reads (from standard input) description of the way the rope is cut,
  • counts how many cuts have to be made in order to get the first shortest piece.
  • writes out the outcome (to standard output)

Input

Ten test cases (given one under another, you have to process all!). Each test case consists of two lines. In the first line there is a number n (1<=n<=20000). In the second line one zero-one word (a sequence of zeros and ones without spaces between them) of length n - the description of the cutting procedure given by Dyzio.

Output

For every testcase your program should write (to the standard output) only one line with one integer equal to the number of cuts which have to be made in order to get the shortest piece.

Example

Input:
9
110011000
[and 9 test cases more]

Output:
4
[and 9 test cases more]

Added by:Adam Dzedzej
Date:2004-06-10
Time limit:1.903s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All
Resource:Internet Contest Pogromcy Algorytmow (Algorithm Tamers)
Round III, 2003

hide comments
2013-05-15 20:40:22 aman jain
can someone explain how to cut and how we get 4 to test case given.
2012-05-03 22:04:05 tainic
Nice! This goes into my list of beautiful problems.
2011-06-06 12:07:18 saluja
I agree it should be three (3)
2011-04-10 09:35:03 Piotr KÄ…kol
@Nazeer Valli - He'll definitely make a great career on SPOJ someday. ;-)
2011-02-18 04:10:00 harib
Jasiek is only 6 and he's writing programs???
2010-07-10 20:29:18 যোবায়ের
@murtuza, 9 is the length of the command sequence.
2010-06-28 10:27:02 murtuza
what do you mean by the shortest piece and what does the 1st input 9, signify here?
2010-04-20 21:52:44 ~!(*(@*!@^&
it is hard to understand problem statements %&^&^(&*(&
2010-01-14 23:42:14 nnn
Why answer on example is 4 and isn't 3?
--2--1-3-2--
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.