Turn S into T

Time limit1sMemory limit128 MB

Summary
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 SS and TT of the same length. SS is made of the characters 0, 1, and ?, while TT is made of 0 and 1. Find the minimum number of operations needed to turn SS into TT.

The allowed operations are:

  1. change a 0 in SS into a 1;
  2. change a ? in SS into a 0 or a 1;
  3. swap the characters at two positions of SS.

For example, if SS is 01??00 and TT is 001010, three operations are enough:

  • start from 01??00;
  • change the 3rd character (a ?) to 1, giving 011?00;
  • change the 4th character (a ?) to 0, giving 011000;
  • swap the 2nd and 5th characters, giving 001010.

Input

The first line contains the number of test cases CC (C≤200C \le 200). Each test case consists of two lines: the first is SS (made of 0, 1, ?) and the second is TT (made of 0, 1). The two strings have equal length, which does not exceed 100100, 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 SS into TT. If it is impossible to turn SS into TT, output -1 as r.

Examples1

  1. Example 1

    Input
    3
    01??00
    001010
    01
    10
    110001
    000000
    
    Expected output
    Case 1: 3
    Case 2: 1
    Case 3: -1