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]的基础上加上二者最大值。

 #include <bits/stdc++.h>

 using namespace std;

 int T = ;

 int main()
{
//freopen("input.txt", "r", stdin);
int iCase;
scanf("%d", &iCase);
while (iCase--) {
string ori, tar;
cin >> ori >> tar; int cnt = , cnt2 = , cnt3 = , cnt4 = ;
int len = ori.length();
for (int i = ; i < len; i++) {
if (ori[i] == '' && tar[i] == '') {
cnt++;
}
else if (ori[i] == '' && tar[i] == '') {
cnt2++;
}
else if (ori[i] == '?') {
if (tar[i] == '') cnt3++;
else cnt4++;
}
} if (cnt + cnt3 < cnt2) {
printf("Case %d: %d\n", T++, -);
}
else {
int ans = cnt3 + cnt4;
ans += max(cnt, cnt2);
printf("Case %d: %d\n", T++, ans);
}
}
return ;
}

UVA12545-Bits Equalizer(思维)的更多相关文章

  1. uva12545 Bits Equalizer

    uva12545 Bits Equalizer You are given two non-empty strings S and T of equal lengths. S contains the ...

  2. 8-3 Bits Equalizer uva12545

    题意: 给出字符串s包含'0' '1' '?'; 再给出字符串t只包含01: 现在我们可以对S做三个操作:把0变成1,把?变成0或1,任意两个位置交换: 问最少操作几次s == t: 贪心 默认除去那 ...

  3. Flip the Bits(思维)

    You are given a positive integer n. Your task is to build a number m by flipping the minimum number ...

  4. 【习题 8-3 UVA - 12545】Bits Equalizer

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 如果1的个数第一个串比第2个串多. 那么就无解. 否则. 找几个位置去凑1 优先找'?'然后才是0的位置 剩余的全都用swap操作就 ...

  5. UVA 12545 Bits Equalizer

    题意: 两个等长的字符串p和q,p有‘0’,‘1’,‘?’组成,q由‘0’,‘1’组成.有三种操作:1.将‘?’变成0:2.将‘?’变成‘1’:3.交换同一字符串任意两个位置上的字符.问有p变到q最少 ...

  6. UVa 12545 Bits Equalizer (贪心)

    题意:给出两个等长的字符串,0可以变成1,?可以变成0和1,可以任意交换s中任意两个字符的位置,问从s变成t至少需要多少次操作. 析:先说我的思路,我看到这应该是贪心,首先,如果先判断s能不能变成t, ...

  7. Bits Equalizer UVA - 12545

    点击打开链接 #include<cstdio> #include<cstring> /* 别看错了:0能变1,1不能变0 能完成的条件是,s与t长度相等且s中0数量和?数量之和 ...

  8. UVa 12545 Bits Equalizer【贪心】

    题意:给出两个等长的字符串,0可以变成1,?可以变成0和1,可以任意交换s中任意两个字符的位置,问从s变成t至少需要多少次操作 先可以画个草图 发现需要考虑的就是 1---0 0---1 ?---0 ...

  9. UVA - 12545 Bits Equalizer (比特变换器)(贪心)

    题意:输入两个等长(长度不超过100)的串S和T,其中S包含字符0,1,?,但T只包含0和1,你的任务是用尽量少的步数把S变成T.有以下3种操作: 1.把S中的0变成1. 2.把S中的“?”变成0或1 ...

随机推荐

  1. 3. mysql性能分析

    一.mysql query optimizer 1. mysql 中有专门负责优化 select 语句的优化器模块,主要功能:通过计算分析系统中收集的统计信息,为客户端的 Query 提供他认为最优的 ...

  2. overall error

    Overall error is same with total error in math.

  3. Android Service解析

    Android Service是一个可以在后台执行长时间运行操作而不提供用户界面的应用组件,它分为两种工作状态,一种是启动状态,主要用于执行后台计算:另一种是绑定状态,主要用于其他组件和Service ...

  4. python 实现微信自动回复(自动聊天)

    原文地址(本人):https://blog.csdn.net/a5878989/article/details/54974249 介绍 微信自动回复其实主要就是登录,接收消息,回复消息三个功能,微信没 ...

  5. laravel框架详解

    一.基础篇 1.概念 Laravel是一个有着美好前景的年轻框架,它的社区充满着活力,同时提供了完整而清晰的文档,而且为快速.安全地开发现代应用提供了必要的功能.2011年,Taylor Otwell ...

  6. 在 ELK Docker 容器中查看,删除索引

    使用 Docker 搭建好 ELK ( https://www.cnblogs.com/klvchen/p/9268510.html ) 环境后,如需查看 elasticsearch 的索引可采取以下 ...

  7. js的map遍历和array遍历

    1. array遍历: [1].forEach() forEach是ES5中操作数组的一种方法,主要功能是遍历数组.forEach方法中的function回调有三个参数:第一个参数是遍历的数组内容,第 ...

  8. python itchat 爬取微信好友信息

    原文链接:https://mp.weixin.qq.com/s/4EXgR4GkriTnAzVxluJxmg 「itchat」一个开源的微信个人接口,今天我们就用itchat爬取微信好友信息,无图言虚 ...

  9. python 正则表达式中反斜杠(\)的麻烦和陷阱

    这里是一点小心得:由于下面两个原因,在正则表达式中使用反斜杠就会产生了一个双重转换的问题. (1).python自身处理字符串时,反斜杠是用于转义字符 (2).正则表达式也使用反斜杠来转义字符     ...

  10. html5常见新增标签

    本文内容: header nav article footer section aside datalist 音频标签: audio 视频标签: video 插入媒体标签: embed 新增input ...