This page is still under construction.

Parts of this page are still being built. What you see may change.

Bits Equalizer

Interview

Time limit2sMemory limit512 MB

Summary
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 SS and TT of the same length. SS consists of the characters 0, 1 and ?, and TT consists of 0 and 1 only. Convert SS into TT using as few moves as possible. One move is one of the following:

  1. change a 0 in SS to 1
  2. change a ? in SS to 0 or 1
  3. swap two characters of SS

No move turns a 1 back into a 0.

For example, take S=S = 01??00 and T=T = 001010. Three moves are enough:

  • at the start S=S = 01??00
  • move 1 changes the third character to 1, so S=S = 011?00
  • move 2 changes the fourth character to 0, so S=S = 011000
  • move 3 swaps the second character with the fifth character, so S=S = 001010

Input

The first line holds the number of test cases CC. (1≤C≤2001 \le C \le 200)

Each test case is two lines. The first line holds the string SS made of 0, 1 and ?. The second line holds the string TT 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 xx is the test case number starting at 1 and yy is the minimum number of moves that turns SS into TT. If no sequence of moves turns SS into TT, print -1 in place of yy.

Examples4

  1. Example 1

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

    Input
    1
    1010
    1010
    
    Expected output
    Case 1: 0
    
  3. Example 3

    Input
    4
    0
    1
    1
    0
    ?
    0
    ?
    1
    
    Expected output
    Case 1: 1
    Case 2: -1
    Case 3: 1
    Case 4: 1
    
  4. Example 4

    Input
    2
    ??????
    101010
    ??????
    000000
    
    Expected output
    Case 1: 6
    Case 2: 6