Bits Equalizer
InterviewTime limit2sMemory limit512 MB
Given S with 0, 1, ? and T with 0 and 1, find the fewest moves (0 to 1, ? to 0 or 1, swap two positions) to turn S into T, or -1.
- Level
Medium5 of 10
- Topics
- Greedy, String, Math, Implementation
- Solved
- No attempts yet
Problem
You are given two strings and of the same length. consists of the characters 0, 1 and ?, and consists of 0 and 1 only. Convert into using as few moves as possible. One move is one of the following:
- change a
0in to1 - change a
?in to0or1 - swap two characters of
No move turns a 1 back into a 0.
For example, take 01??00 and 001010. Three moves are enough:
- at the start
01??00 - move 1 changes the third character to
1, so011?00 - move 2 changes the fourth character to
0, so011000 - move 3 swaps the second character with the fifth character, so
001010
Input
The first line holds the number of test cases . ()
Each test case is two lines. The first line holds the string made of 0, 1 and ?. The second line holds the string made of 0 and 1. The two strings have the same length, which is between 1 and 100.
Output
For each test case, print one line in the format Case x: y, where is the test case number starting at 1 and is the minimum number of moves that turns into . If no sequence of moves turns into , print -1 in place of .