SPOJ DYZIO

The cutting of the string can be visualized as the pre order traversal of a binary tree.

Assume that your position is in the root just before you have started reading the input. Now start reading the input string. If the current character is '1', visit the left subtree and the right subtree one after another. A '0' character means you have reached a leaf (the sub-tree is empty).

Do this recursively. You just have to count the number of nodes you have to visit until reaching the first deepest leaf of the tree.

the pseudo code would be something like this:

VISIT(depth)  /* call this function with VISIT(0) */ if depth > deepest deepest = depth ans = cut_no    /* initially cut_no will be 0 */ if input[current_position] == 1 cut_no = cut_no + 1 current_position = current_position + 1 VISIT(depth + 1) VISIT(depth + 1) else return