zoukankan      html  css  js  c++  java
  • UVA12545-Bits Equalizer(思维)

    Problem UVA12545-Bits Equalizer

    Accept: 821  Submit: 4548
    Time Limit: 3000 mSec

    Problem Description

    Input

    The first line of input is an integer C (C ≤ 200) that indicates the number of test cases. Each case consists of two lines. The first line is the string S consisting of ‘0’, ‘1’ and ‘?’. The second line is the string T consisting of ‘0’ and ‘1’. The lengths of the strings won’t be larger than 100.

     Output

    For each case, output the case number first followed by the minimum number of moves required to convert S into T. If the transition is impossible,output ‘-1’ instead.
     

     Sample Input

    3
    01??00
    001010
    01
    10
    110001
    00000
     

     Sample Output

    Case 1: 3

    Case 2: 1

    Case 3: -1

    题解:这个题还是挺不错的,结论很简洁,但是不是太好想。首先明确一点就是要进行几步操作和字符串的顺序无关,只和对应位置的对应情况有关,一共由四种对应:

    1、0 --> 1

    2、1 --> 0

    3、?--> 1

    4、?--> 0

    分别记录个数为cnt[1~4],首先cnt[3、4]是肯定要加进去的,只需考虑别的步骤,2这种对应只能通过交换来消除,所以如果cnt1+cnt3<cnt2,就是无解的,否则答案就是在原来的基础上加上max(cnt1, cnt2).

    解释一下,如果cnt1 >= cnt2,那么只用1这种对应来交换的就已经足够了,还需要把多出来的1对应变成正确对应因此此时加上cnt1,如果cnt1 < cnt2,这时需要用3对应来配,此时需要cnt2步操作,因此就是在cnt[3、4]的基础上加上二者最大值。

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 int T = 1;
     6 
     7 int main()
     8 {
     9     //freopen("input.txt", "r", stdin);
    10     int iCase;
    11     scanf("%d", &iCase);
    12     while (iCase--) {
    13         string ori, tar;
    14         cin >> ori >> tar;
    15 
    16         int cnt = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0;
    17         int len = ori.length(); 
    18         for (int i = 0; i < len; i++) {
    19             if (ori[i] == '0' && tar[i] == '1') {
    20                 cnt++;
    21             }
    22             else if (ori[i] == '1' && tar[i] == '0') {
    23                 cnt2++;
    24             }
    25             else if (ori[i] == '?') {
    26                 if (tar[i] == '1') cnt3++;
    27                 else cnt4++;
    28             }
    29         }
    30 
    31         if (cnt + cnt3 < cnt2) {
    32             printf("Case %d: %d
    ", T++, -1);
    33         }
    34         else {
    35             int ans = cnt3 + cnt4;
    36             ans += max(cnt, cnt2);
    37             printf("Case %d: %d
    ", T++, ans);
    38         }
    39     }
    40     return 0;
    41 }
  • 相关阅读:
    最小的K个数
    数组中出现次数超过一半的数字
    合并排序
    一只小蜜蜂...
    从零开始学springboot笔记(三)-Spring boot之热部署之Springloaded(仅做了解,实际开发中不用它)
    从零开始学springboot笔记(二)-Spring boot返回json数据(中文无乱码)
    从零开始学springboot笔记(一)-Spring boot之Hello Word
    IDEA2018永久破解版下载与安装
    $.ajaxSettings.async = false;是什么意思?
    嘿嘿
  • 原文地址:https://www.cnblogs.com/npugen/p/9683662.html
Copyright © 2011-2022 走看看