http://www.spoj.com/problems/EMTY2/
I suggest using C++ STL stack for this one. If you need more info on stack go here. Here is how we will do it. We loop over our string, if the current element is '1' then we add it to our stack, if not then we need to check for the last 2 elements, if the last 2 elements are 1 and 0 ( in other words the last two and the current one are forming 100) then we pop those elements otherwise we push the current '0' to our stack. In the end we check if there are any elements left in the stack if there are then the answer is 'no' otherwise is 'yes'.
I suggest using C++ STL stack for this one. If you need more info on stack go here. Here is how we will do it. We loop over our string, if the current element is '1' then we add it to our stack, if not then we need to check for the last 2 elements, if the last 2 elements are 1 and 0 ( in other words the last two and the current one are forming 100) then we pop those elements otherwise we push the current '0' to our stack. In the end we check if there are any elements left in the stack if there are then the answer is 'no' otherwise is 'yes'.
No comments:
Post a Comment