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?) )