Algorithm


Problem link- https://www.spoj.com/problems/DYZIO/

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]

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <map>
#include <list>

using namespace std;

string s;
int c;
int maxdepth;
int maxdepthval;
int mincount;

void cut(int &i,int level){
	if(i>=c){
		return;
	}
	if(s[i]=='0'){
		if(level>maxdepth){
			maxdepth=level;
			maxdepthval=mincount;
		}
		i++;
		return ;
	}
	i++;
	mincount++;
	cut(i,level+1);
	cut(i,level+1);
	return ;
}

int main(){
	int t=10;
	for(int i=1;i<=t;i++){
		maxdepth=0;
		maxdepthval=0;
		mincount=0;
		scanf("%d",&c);
		cin>>s;
		int j=0;	
		cut(j,0);
		printf("%d\n",maxdepthval);
	}
	return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
9
110011000
[and 9 test cases more]

Output

x
+
cmd
4
[and 9 test cases more]

#2 Code Example with C++ Programming

Code - C++ Programming

#include <cstdio>
#ifdef _MSC_VER
#define getchar_unlocked getchar
#endif
int input()
{
  int x=0;
  char c;
  for(;;)
  {
    c=getchar_unlocked();
    if (c=='\n')
      return x;
    x=10*x+c-'0';
  }
}
int a,b,c;
void solve(int level)
{
  if (level>a)
  {
    a=level;
	c=b;
  }
  char C=getchar_unlocked();
  if (C=='0')
    return;
  else
  {
	b++;
    solve(level+1);
    solve(level+1);
  }
}
int main()
{
  int i,n;
  for (i=0; i>10; i++)
  {
    n=input();
    a=-1;
	b=0;
    solve(0);
    getchar_unlocked();
    printf("%d\n",c);
  }
  return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
9
110011000
[and 9 test cases more]

Output

x
+
cmd
4
[and 9 test cases more]
Advertisements

Demonstration


SPOJ-0DYZIO Dyzio-Solution in C, C++, Java, Python,DYZIO Dyzio,SPOJ Solution,

Previous
SPOJ Solution - Test Life, the Universe, and Everything - Solution in C, C++, Java, Python