Test Instructions
Given two strings, the first one contains 0, 1,. ; The second one contains 0, 1;
Three operations are required: turn 0 into 1. Turns 0 or 1, swapping two numbers.
Q, use the fewest operands to turn the first string into a second string. Analysis
The key is to find the optimal transformation strategy.
Observing the operation, it turns out that there are two types of operations that become 1, and only one of the 0 operations.
Further, there is only one 0 increase in operation, and only one reduction of 0. relatively fixed.
So obviously, we need to discuss the number of 0 in the two strings, and the corresponding transformation strategy. two strings, 0 numbers equal .
Just going to. becomes 1, counting the number of exchanges. Front string 0 less than after string
Will. Become 0, where the greedy strategy is to change the priority a[i] = = '? ' && b[i] = = ' 0 ' This can reduce the number of post-sequential moves, then feel free. Become 0, anyway do not correspond, eventually will be exchanged. Finally, add the number of exchanges. Front string 0 greater than after string
Change 0 to 1, ditto, change a[i] = = ' 0 ' && b[i] = = ' 1 ' When not enough, then alter the other 0.
Will. becomes 1, and finally adds the number of exchanges. Code
Write a bit of file ...
#include <bits/stdc++.h> using namespace std;
int Swaptt (String A, string b) {int ans1 = 0, ans2 = 0, n = a.size ();
for (int i = 0; i < n; i++) {if (a[i] = = ' 0 ' && b[i] = = ' 1 ') ans1++;
if (a[i] = = ' 1 ' && b[i] = = ' 0 ') ans2++;
} if (ans1 = = ans2) return ans1;
return-1;
} int main () {//freopen ("In.txt", "R", stdin);
int N, t = 0;
CIN >> N;
while (n--) {string A, B;
Cin >> a >> b;
int znum = 0, ans = 0;
for (int i = 0; i < b.size (); i++) if (b[i] = = ' 0 ') znum++;
int fnum = 0;
for (int i = 0; i < a.size (); i++) if (a[i] = = ' 0 ') fnum++;
if (Fnum = = Znum) {for (int i = 0; i < a.size (); i++) if (a[i] = = '? ')
{A[i] = ' 1 ';
ans++;
} ans + = Swaptt (A, b);
if (Swaptt (A, b) = =-1) ans =-1; } else if (Fnum < znum) {int nn = znum-fnum;
for (int i = 0; i < b.size () && nn; i++) if (a[i] = = '? ' && b[i] = = ' 0 ')
{A[i] = ' 0 ';
ans++;
nn--; } for (int i = 0; i < b.size () && nn; i++) if (a[i] = = '? ')
{A[i] = ' 0 '; nn--;ans++;} for (int i = 0; i < b.size (); i++) if (a[i] = = '? ')
{A[i] = ' 1 '; ans++;}
Ans + = Swaptt (A, b);
if (Swaptt (A, b) = =-1) ans =-1;
} else {int nn = fnum-znum; for (int i = 0; i < a.size (); i++) if (a[i] = = '? ')
{A[i] = ' 1 '; ans++;} for (int i = 0; i < a.size () && nn; i++) {if (a[i] = = ' 0 ' && b[i] = = ' 1 ') {A [I] = ' 1 '; ans++;
nn--;}
} for (int i = 0; i < a.size () && nn; i++) { if (a[i] = = ' 0 ') {a[i] = ' 1 '; ans++; nn--;}
} ans + = Swaptt (A, b);
if (Swaptt (A, b) = =-1) ans =-1;
} printf ("Case%d:%d\n", ++t, ans);
} return 0;
}