12545-bits equalizer (greedy?) )

Source: Internet
Author: User

This problem I am my own idea, once a, 0.000ms. Look at the online most of the solution is similar, I would like to talk about my ideas.

Assume that the string a, b

All we have to do is sweep it backwards. Jump over, encounter a[i] = ' 0 ' &&a[i]!=b[i]; then look back for a a[j]== ' 1 ' &&a[i]!=b[j], that is to say, the first exchange of this skill to compare the provincial steps. If there's nothing to swap, then sweep again? What if a[i]==? && B[i]==0 So obviously we can start with? becomes 1 again and A[i] exchange, need two steps, if still not satisfied, that can only directly put A[i] into 1

A[i]==1 && A[i]!=b[i] The situation is similar to the above, but if the first two can not be satisfied, then this sequence can not become a B sequence

So it's good to start thinking clearly and then knock the code down, sorting it out.

The code is as follows:

#include <bits/stdc++.h>using namespace Std;int T,MAXN = 0;char a[105],b[105];int main () {scanf ("%d", &t);        while (t--) {scanf ("%s%s", A, b);        printf ("Case%d:", ++MAXN);        int n = strlen (a);        bool OK = true;        int cnt = 0;            for (int i=0;i<n;i++) {if (a[i]== '? ') continue;                else if (a[i]!=b[i]&&a[i]== ' 0 ') {bool flage = false; for (int j=i+1;j<n;j++) {if (a[j]!=b[j]&&a[j]== ' 1 ') {flage = true; a[j] = ' 0 '; a[i] = ' 1 '; CN t++; Break  }} if (!flage) {for (int j=0;j<n;j++) {if (A[j] == '?'  &&b[j]== ' 0 ') {flage = true; a[j] = ' 0 '; a[i] = ' 1 '; cnt+=2; Break                }} if (!flage) {a[i] = ' 1 '; cnt++;}                }} else if (a[i]!=b[i]&&a[i]== ' 1 ') {bool flage = false; for (int j=i+1;j<n; j + +) {if (a[j]!=b[j]&&a[j]== ' 0 ') {flage = true; A[j] = ' 1 '; a[i] = ' 0 '; cnt++; break;} } if (!flage) {for (int j=0;j<n;j++) {if (a[j]== '? ')                    &&b[j]== ' 1 ') {flage = true; A[j] = ' 1 '; a[i] = ' 0 '; cnt+=2; break;}            if (!flage) {ok = false; break;}//Cannot Be transformed}}} if (OK) { for (int i=0;i<n;i++) if (a[i] = = '? ') cnt++;        Don't forget here printf ("%d\n", CNT);    } else printf (" -1\n"); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

12545-bits equalizer (greedy?) )

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.