Turn S into T
Time limit1sMemory limit128 MB
Given strings S (with 0,1,?) and T (0,1), compute the minimum number of change/swap operations to transform S into T, or -1 if impossible.
- Level
Medium6 of 10
- Topics
- Greedy, String, Simulation
- Solved
- No attempts yet
Problem
You are given two strings and of the same length. is made of the characters 0, 1, and ?, while is made of 0 and 1. Find the minimum number of operations needed to turn into .
The allowed operations are:
- change a
0in into a1; - change a
?in into a0or a1; - swap the characters at two positions of .
For example, if is 01??00 and is 001010, three operations are enough:
- start from
01??00; - change the 3rd character (a
?) to1, giving011?00; - change the 4th character (a
?) to0, giving011000; - swap the 2nd and 5th characters, giving
001010.
Input
The first line contains the number of test cases (). Each test case consists of two lines: the first is (made of 0, 1, ?) and the second is (made of 0, 1). The two strings have equal length, which does not exceed , and neither is empty.
Output
For each test case, output a line Case x: r, where x is the test case number (starting from 1) and r is the minimum number of operations needed to turn into . If it is impossible to turn into , output -1 as r.