首页 > 代码库 > UVa12545 Bits Equalizer (贪心)

UVa12545 Bits Equalizer (贪心)

链接:http://bak2.vjudge.net/problem/UVA-12545

 

分析:贪心乱搞。

 1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4  5 const int maxn = 100 + 5; 6  7 char s[maxn], t[maxn]; 8  9 int main() {10     int T;11     scanf("%d", &T);12     for (int kase = 1; kase <= T; kase++) {13         scanf("%s%s", s, t);14         int pr01 = 0, pr10 = 0, prq0 = 0, prq1 = 0;15         for (int i = 0; i < strlen(s); i++) {16             if (s[i] == 0 && t[i] == 1) pr01++;17             if (s[i] == 1 && t[i] == 0) pr10++;18             if (s[i] == ? && t[i] == 0) prq0++;19             if (s[i] == ? && t[i] == 1) prq1++;20         }21         int ans = 0;22         while (pr01 && pr10) {23             pr01--; pr10--;24             ans++;25         }26         while (pr10 && prq1) {27             pr10--; prq1--;28             ans += 2;29         }30         if (pr10) ans = -1;31         else ans += pr01 + prq0 + prq1;32         printf("Case %d: %d\n", kase, ans);33     }34     return 0;35 }

 

UVa12545 Bits Equalizer (贪心)