BANDW - Black and White

no tags 

Original statement in spanish at

The famous game Black and White is a solitaire which is played using a set of identical chips. Each chip has two faces with different colors. Surprisingly enough, those colors are black and white.

The game starts by placing N chips forming a single line. There exists an objective pattern which is a given sequence of N colors black or white. In a single move, the player can choose a group of consecutive chips and invert their color, in other words, for each chip in the group, the color which was facing up, is facing down and the one which was facing down is facing up. The game finishes when the facing up colors of the chips are equal to the objective pattern.

Barby has just discovered this game and soon she realized that you can always won by inverting each chip individually if needed. To make this game more challenging to her, she wanted to win in the least possible number of moves. Note that Barby just cares about how many moves she makes, and it doesn't matter how many chips are inverted in each move. To know how well is Barby playing, she asked you to make a program that given the chip's initial position and the objective pattern, shows the least possible number of moves to win the game. Are you going to say no?


The input contains several test cases. Each test case is described in a single line that contains two non-empty words S and T of equal size and at most 500 letters each. S indicates the chip's initial position while T represents the objective pattern. Both words only contain uppercase letters "B" and "N", representing respectively White and Black. The last line of the input contains two asterisks separated by a single space and should not be processed as a test case.


For each test case output a single line with an integer representing the least possible number of moves such that you can change the chip's which are initially positioned as described in S to form the pattern given by T.



* *



hide comments
rsd_unleashed: 2015-05-19 10:18:17

Nice one :)

no name: 2015-03-27 08:36:24

huh... easy one :)

Last edit: 2015-03-27 08:39:40
Rajat (1307086): 2015-03-20 00:39:16

It would have been a DP problem if we were allowed to change both the chips. ;)

MKM: 2015-02-04 09:57:41

\0 doesn't work o_O

:.Mohib.:: 2014-12-30 18:22:21

Easy one.... :)

Maroof: 2014-12-12 11:27:38

Accepted in 1st Go :) :)

jaspreet kaur: 2014-12-11 21:14:47

AC in one go..easy one :-)

Crazy Monk : 2014-12-04 19:42:43

EASY ONE !! MY 50th :D

Sumit Gulati: 2014-09-27 22:46:03

How is this ans for first test case is 3.. I dint got it :(

pandu ranga rao: 2014-07-17 15:11:28

cake walk...:P

Added by:Pablo Ariel Heiber
Time limit:2s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Argentinian Programming Tournament 2011