12545-Bits Equalizer贪心

比较蛋疼贪心,具体除非S在位置i为 ?并且 T在 i 为 1,负责S为?的全换成0

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<cmath>
typedef long long LL;
using namespace std;
#define MAXD 200 + 10
int main(){
    int T,Case = 1;
    scanf("%d",&T);
    while(T--){
         char str1[MAXD],str2[MAXD];
         int a1 = 0,b1 = 0,c1 = 0;  /*分别代表0,1,问号的个数*/
         int a2 = 0,b2 = 0;     /*分别代表0,1,问号的个数*/
         scanf("%s",str1);
         scanf("%s",str2);
         printf("Case %d: ",Case++);
         int L = strlen(str1);
         for(int i = 0 ; i < L ; i++){
             if(str1[i] == '0') a1 ++;
             else if(str1[i] == '1') b1 ++;
             else if(str1[i] == '?') c1 ++;
         }
         for(int i = 0 ; i < L ; i++){
             if(str2[i] == '0') a2 ++;
             else
             if(str2[i] == '1') b2 ++;
         }
         if(c1 + a1 < a2){
           printf("-1\n");
           continue;
         }
         /*先将问号变成缺少的0,优先选择对应位置也是0的位置变*/
         int zero = a2 - a1;
         int cnt = 0;
         /*zero是str1比str2少的0的个数*/
         if(zero > 0){
             /*先改相同位置为0的?*/
             for(int i = 0 ; i < L && zero > 0; i++){
                 if(str1[i] == '?' && str2[i] == '0'){
                     str1[i] = '0';
                     zero--;
                     cnt++;
                 }
             }
             for(int i = 0 ; i < L && zero > 0 ; i ++)if(str1[i] == '?'){
                  str1[i] = '0';
                  cnt ++;
                  zero--;
             }
             for(int i = 0 ; i < L ; i++)if(str1[i] == '?' && str2[i] == '1'){
                 str1[i] = '1';
                 cnt ++;
             }
             for(int i = 0 ; i < L ; i++)if(str1[i] == '?'){
                 str1[i] == '0';
                 cnt ++;
             }
             int x = 0,y = 0;
             for(int i = 0 ; i < L ; i++)
             if((str1[i] == '1')&&(str2[i]) == '0')
             x++;
             else if((str1[i] == '0')&&(str2[i] == '1'))
             y++;
             cnt += max(x,y);
             printf("%d\n",cnt);
         }
         else { /*如果str1的0比str2的多*/
             for(int i = 0 ; i < L ; i++)if((str1[i] == '?') && (str2[i] =='1')){
                 cnt ++;
                 str1[i] = '1';
             }
             for(int i = 0 ; i < L ; i++)if(str1[i] == '?'){
                 cnt ++;
                 str1[i] = '0';
             }
             int x = 0,y = 0;
             for(int i = 0 ; i < L ; i++)
             if((str1[i] == '1')&&(str2[i]) == '0')
             x++;
             else if((str1[i] == '0')&&(str2[i] == '1'))
             y++;
             cnt += max(x,y);
             printf("%d\n",cnt);
         }
    }
    return 0;
}


你可能感兴趣的:(12545-Bits Equalizer贪心)