#include <bits/stdc++.h>#defineREP (I,A,B) for (int i=a;i<=b;i++)#defineMS0 (a) memset (A,0,sizeof (a))using namespaceStd;typedefLong Longll;Const intmaxn=1000100;Const intinf=1<< in;CharS[MAXN],T[MAXN];intLen;intc10,c01,c1,c0;intMain () {Freopen ("In.txt","R", stdin); intT;cin>>u; intcasen=1; while(t--) {scanf ("%s%s", s,t); Len=strlen (s); C10=c01=c1=c0=0; REP (i,0, len-1){ if(s[i]=='0'&&t[i]=='1') c01++; if(s[i]=='1'&&t[i]=='0') c10++; if(s[i]=='?'&&t[i]=='0') c0++; if(s[i]=='?'&&t[i]=='1') c1++; } //cout<<c01<< "<<c10<<" "<<c0<<" "<<c1<<endl; intans=0; if(c01>=C10) {ans+=C10; C01-=C10; Ans+=c01+c1+C0; } Else{ans+=C01; C10-=C01; if(c1>=C10) {ans+=c10*2; C1-=C10; Ans+=c1+C0; } Elseans=-1; } printf ("Case %d:%d\n", casen++, ans); } return 0;}
View Code
Greedy, sort the discussion about determining the priority of the operation is fine.
UVA 12545 Bits Equalizer