BANDW - Black and White

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?

Input

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.

Output

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.

Example

Input:

BBNBBNBBBB NNNNNBBNNB
BNBNB NBNBN
BNBN NBNB
B B
* *

Output:

3
1
1
0

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

hide comments
2014-06-05 18:35:01 `Ak
hey u can use cout cin also i got AC with it :)
2014-05-12 13:23:41 jetpack
any tricky test cases plz
2014-01-24 11:38:59 paras meena
It is not DP Problem :(
2014-01-12 15:03:39 785227
Extremely easy :)
2013-12-10 05:07:15 Jumpy
i think u were getting TLE while using cin and cout...@anmol bcz cin/ cout take more time than scanf/printf
2013-11-25 19:35:28 Anmol Pandey
Got AC after changing cout/cin to printf/scanf...i dont understand ...can anyone tell me..it is my genuine doubt
2013-09-01 17:04:50 hiddenman
nice 1......
test cases
NBBN NBNB
BNBN NBBN
* *
ANS
1
1
2013-08-31 08:42:53 Ayush Vatsa
easy 1...
2013-07-06 07:14:29 rahul kumar singh
Some silly mistakes n 4 WA...A bit tricky..A bit
2013-06-26 07:16:35 wa
@AA kinky
replace "gets(str)" with "scanf("%s", &str)" and include header file "#include <stdio.h>".it will work and plz remove the link of ur code

Last edit: 2013-06-26 07:18:42
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.