The One Where Joey Dates Rachel
Everybody is sad, most of all Monica as Chandler must spend Christmas and New Years in Tulsa, where his staff is all depressed they can't be with their families. He arranged for the head office to send a bonus, but even that's another bummer: donations in their name to the New York ballet. Chandler sends a message to the head office in the form of a binary code, which somehow gets changed on the way.
Chef is given two binary strings and of lengths and respectively. We can perform the following operations on string .
- Select two adjacent characters of the string and flip these two characters. ( Flipping the character is equivalent to replacing the character with , and vice-versa )
- Add a character, either or to any of the ends ( either the front or the back ) of the string .
Find the minimum number of operations needed to convert to . If it is impossible to convert to , print -1.
Input Format
- First line will contain , number of testcases. Then the testcases follow.
- First line of each testcase contains of a single line of input, two integers .
- Second line of each testcase contains a binary string .
- Third line of each testcase contains a binary string .
Output Format
For each testcase, output in a single line the minimum number of operations needed to convert to . If it is impossible to convert, print -1.
Constraints
- contain only.
Sample Input 1
2
3 3
101
000
4 3
0100
000
Sample Output 1
2
-1
Explanation
Test case 1: Flip the first two bits in one move and second two bits in second move. .
Test case 2: It is impossible to convert to using any sequence of moves.
Comments
Post a Comment