UVa 12545 Bits Equalizer (greedy)

Source: Internet
Author: User

Test instructions: Give two equal length strings, 0 can become 1,? can become 0 and 1, can arbitrarily swap the position of any two characters in S, ask at least how many times to change from S to T.

Analysis: First of all my ideas, I see this should be greedy, first of all, if the first Judge S can become T, calculate T in 1 and s in 1.

And then figure out how much more t than S 1, and then first consider putting? Become 1 is the best, if not enough can only turn 0 into 1, must not be put? becomes 1, because it takes two steps,

is not optimal, and turning 0 into 1 is a step. And put the rest? becomes 1 (if 1 is not enough) or 0 (1 enough). The last is to calculate the number of times to be exchanged,

This as long as the number of different locations can be, do not forget 1 may not be enough, this also to calculate, how to calculate, think about, first turn 0 into 1, then exchange,

So we need to add 1 to the change, then add it in different places and divide by 2. The sum is the result.

However, I feel that I write too bad, the internet Baidu, see the Great God is written is concise and clear ....

So, first of all, the exchange is optimal, because one operation can meet two, and then go to find 1-0 and? -1 of the exchange, for what? Because we look at the 1 in S is not much,

If less, it doesn't matter, can directly put? Become 1, if there is no solution to more, so to compare this. And finally add? -0, just one step.

The great God is the King.

The code is as follows:

This is my code.

#include <cstdio> #include <cstring> #include <iostream>using namespace Std;const int maxn = + 10;cha    R S[maxn], T[maxn];int main () {//Freopen ("In.txt", "R", stdin);    int T;    Cin >> T;        for (int kase = 1; kase <= T; ++kase) {scanf ("%s%s", S, T);        int n = strlen (s);    int cnts1 = 0, cntt0 = 0, Cnts0 = 0;//cnts1 is the number of 1 in the S string, and the other for (int i = 0; i < n; ++i) {if (' 0 ' = = t[i])            ++cntt0;            if (' 0 ' = = s[i]) ++cnts0;        else if (' 1 ' = = S[i]) ++cnts1;         } int cntt1 = number of n-cntt0;//1 int cntss = n-cnts0-cnts1;//? The number of int det = cntt1-cnts1;//s and T difference how much 1        printf ("Case%d:", Kase); if (Det < 0) {printf (" -1\n"); continue;}        1 Too many, the question mark all changed not enough int cnt = 0;  for (int i = 0; i < n; ++i) if (Det && '? ' = = S[i] && t[i] = = ' 1 ') {++cnt; S[i] = ' 1 '; The--det;//?   becomes 1} for (int i = 0; i < n; ++i)//turns 0 to 1         if (Det && ' 0 ' = = s[i] && ' 1 ' = = T[i]) {--det;  S[i] = ' 1 ';            ++cnt; } for (int i = 0; i < n; ++i) {//Put the rest?  becomes 0 or 1 if (Det && '? ' = = S[i]) {++cnt;  S[i] = ' 1 '; --det;  } if (!det && '? ' = = S[i]) {++cnt;  S[i] = ' 0 ';        }} int x = 0;        for (int i = 0; i < n; ++i)//Calculate different number, if (s[i]! = T[i]) ++x;        CNT + = det;//calculates the number of times to be exchanged cnt + = (x + det)/2;    printf ("%d\n", CNT); } return 0;}

Here are my references to the codes of the Great gods:

#include <cstdio> #include <cstring> #include <iostream>using namespace Std;const int maxn = + 10;cha    R S[maxn], T[maxn];int main () {int t;    Cin >> T;        for (int kase = 1; kase <= T; ++kase) {scanf ("%s%s", S, T);        int n = strlen (s);        int zero_one = 0, One_zero = 0, Q_one = 0, Q_zero = 0;            for (int i = 0; i < n; ++i) {if (s[i] = = ' 0 ' && t[i] = = ' 1 ') ++zero_one;            else if (s[i] = = ' 1 ' && t[i] = = ' 0 ') ++one_zero;            else if (s[i] = = '? ' && t[i] = = ' 0 ') ++q_zero;        else if (s[i] = = '? ' && t[i] = = ' 1 ') ++q_one;        } int cnt = 0;   while (Zero_one && One_zero) {--zero_one;            --one_zero;        ++cnt;   } while (Q_one && One_zero) {--q_one;            --one_zero;        CNT + = 2;        } if (One_zero) cnt =-1;        else cnt + = Q_zero + Zero_one + q_one; printf ("Case%d:%d\n", Kase, CNT); } return 0;}

UVa 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.