uva6152Bits Equalizer

简介: View Code 1 #include 2 #include 3 #include 4 #include 5 #include 6 using namespace std; 7 #define DEBUG 8 int min(int a, int ...
View Code
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string>
 4 #include <math.h>
 5 #include <algorithm>
 6 using namespace std;
 7 #define DEBUG
 8 int min(int a, int b){
 9     return a<b?a:b;
10 }
11 int main(){
12 #ifndef DEBUG
13     freopen("in.txt", "r", stdin);
14 #endif
15     int cas;
16     scanf("%d", &cas);
17     int id=1;
18     while(cas--){
19         string s, t;
20         cin >> s >> t;
21         printf("Case %d: ",id++);
22         int i, ans=0;
23         int cnt1, cnt2, cnt3, cnt4, cnt5, cnt6;
24         cnt1=count(s.begin(), s.end(), '0');
25         cnt2=count(s.begin(), s.end(), '?');
26         cnt3=count(t.begin(), t.end(), '0');
27         if(cnt1+cnt2<cnt3){
28             printf("%d\n", -1);
29             continue;
30         }
31         cnt4=0;
32         cnt5=0;
33         ans+=cnt2;
34         for(i=0; i<s.length(); i++){
35             if(s[i]=='0' && t[i]=='1') cnt4++;
36             else if(s[i]=='1' && t[i]=='0') cnt5++;
37         }
38         ans+=min(cnt4, cnt5);
39         cnt6=abs(cnt4-cnt5);
40         ans+=cnt6;
41         printf("%d\n", ans);
42     }
43     return 0;
44 }

题目很简答,题意就不说了。

这里值得说的是,stl的count函数在这里可以用一下~

目录
相关文章
|
存储 C语言
【CSAPP随笔】CH3:Bits, Bytes, and Integers
【CSAPP随笔】CH3:Bits, Bytes, and Integers
74 0
hdu 1196 Lowest Bit(水题)
hdu 1196 Lowest Bit(水题)
40 0
uva127 "Accordian" Patience
uva127 "Accordian" Patience
42 0
UVa343 What Base Is This
UVa343 What Base Is This
47 0
uva375 Inscribed Circles and Isosceles Triangles
uva375 Inscribed Circles and Isosceles Triangles
39 0
LeetCode 191 Number of 1 Bits
题目描述: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).
1008 0
|
机器学习/深度学习
[LeetCode] Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1’ bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11’ has binary representation
970 0
|
机器学习/深度学习
uva 12470 Tribonacci
点击打开uva12470  思路: 矩阵快速幂 分析: 1 裸题 代码: /************************************************ * By: chenguolin ...
989 0