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 }
  • 相关阅读:
    lombok 下的@Builder注解用法
    吉特日化MES实施--三种浪费
    吉特日化MES配料工艺参数标准版-第二版
    吉特日化MES系统&生产工艺控制参数对照表
    吉特日化MES & SQL Server 无法执行数据库脚本
    吉特日化MES系统--通过浏览器调用标签打印
    吉特日化MES&WMS系统--三色灯控制协议转http
    吉特仓储管理系统-库存管理分类汇总
    “千言数据集:文本相似度”权威评测,网易易智荣登榜首
    网易有数品牌升级:聚焦数据价值,助力企业数字化创新
  • 原文地址:https://www.cnblogs.com/npugen/p/9683662.html
Copyright © 2011-2022 走看看