UVA 12545 Bits Equalizer

Source: Internet
Author: User

Title Link: http://www.bnuoj.com/bnuoj/problem_show.php?pid=27865

Bits EqualizerTime Limit:1000msMemory Limit:131072KB This problem is judged on UVA. Original id:12545
64-bit integer IO format: %lldJava class Name: MainPrevSubmitStatusStatisticsdiscussNext Font Size: +-

[PDF Link]

You are given non-empty strings S and T of equal lengths. S contains the characters '0', '1' and '?', whereas T contains ' 0' and ' 1' only. Your task is to convert S into T in minimum number of moves. In each move, you can

    1. Change a '0' in S to '1'
    2. Change a '?' in S to '0' or '1'
    3. Swap any of characters in S

As an example, suppose S = "01??" XX"and T ="001010". We can transform S into T 3 moves:

    • Initially S = "01?? XX"
    • -Move 1:change S[2] to '1'. S becomes "011?00"
    • -Move 2:change S[3] to '0'. S becomes "011000"
    • -Move 3:swap s[1] with s[4]. S becomes "001010"
    • S is now equal to T

Input

The first line of input was an integer C (C-All) that indicates the number oftest Cases. Each case consists of lines. The first line is the string S consisting of '0', '1' and '?'. The second line is the string T consisting of '0' and '1'. The lengths of the strings won ' t be larger than.

Output

For each case, output the case number first followed by the minimum number of moves required to convert S Into T. If the transition is Impossible,output '-1' instead.

Sample Input

301?? 000010100110110001000000

Sample Output

Case 1:3case 2:1case 3:-1

Sourcewestern and Southwestern European Regionals, 2012-valencia title: The first character corresponds to each position of the second character, and at least the number of times it needs to be moved. There are the following rules: 1, 0 can become 1; 2. Can be changed to 0 or 1;3, any 0 and 1 position can be interchangeable problem-solving ideas: Unity 0 and 1 and? The number, because? Must be transformed, so the minimum number of moves also have to have? Several times, so on the basis of the addition can be. In two cases consider: (switching, transformation) Priority transformation Situation: 1, exists 1/0, and can be found behind 0/1, then Exchange. 2, exists 1/0, but cannot find its corresponding 0/1 can be exchanged, then can only choose? /1, then swap. Transformation situation: 1, the existence of 0/1, the case will be directly 0 transform into 1 can. In general, as long as all the circumstances to consider, AC is no longer a dream, wow haha ~ See the code.
1#include <iostream>2#include <cstdio>3#include <cstring>4 5 using namespacestd;6 7 intMain ()8 {9     intn,len1,flag=1;Ten     Chars[ the],t[ the]; Onescanf"%d",&n); A      while(n--) -     { -scanf"%s%s", s,t); thelen1=strlen (s); -         //Len2=strlen (t); -         intA,B,C,AA,BB; -a=b=c=aa=bb=0; +          for(intI=0; i<len1; i++) -         { +             if(s[i]=='0') Aa++; at             Else if(s[i]=='1') -b++; -             Else -C++; -         } -          for(intI=0; i<len1; i++) in         { -             if(t[i]=='0') toaa++; +             Else -bb++; the         } *         if(b>BB) $         {Panax Notoginsengprintf ("Case %d: -1\n", flag++); -             Continue; the         } +         intsum=0; A         intJ; the         if(s[i]=='1'&&t[i]=='0') +         { -              for(j=0; j<len1; J + +) $             { $                 if(s[j]=='0'&&t[j]=='1') -                 { -sum++; thes[i]='0'; -s[j]='1';Wuyi                      Break; the                 } -             } Wu             if(j>=len1) -                  Break; About  $         } -     } -      for(intI=0; i<len1; i++) -     { A         intJ; +         if(s[i]=='1'&&t[i]=='0') the         { -              for(j=0; j<len1; J + +) $             { the                 if(s[j]=='?'&&t[j]=='1') the                 { thesum++; thes[i]='?'; -s[j]='1'; in                      Break; the                 } the             } About             if(j>=len1) the                  Break; the         } the     } +      for(intI=0; i<len1; i++) -     { the         if(s[i]=='0'&&t[i]=='1')Bayisum++; the         //Ans=sum+c; the     } -printf ("Case %d:%d\n", flag++,sum+c); - } the return 0; the}

UVA 12545 Bits Equalizer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.