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. Spring Bean的生命周期相关博客

    最近得面试题一直 问 Spring 得生命周期,鉴于自己还未阅读过源码 所以只能是自己 背一波了.属实不懂硬背得作用,但是无奈被各位面试官打败了.等以后有时间了 一定要阅读几遍spring的 源码 有 ...

  2. javascript html页面中的内容替换

    <script language="javascript"> function ffRed(){  var xsxf = document.getElementById ...

  3. P1993 小 K 的农场

    题目描述 小 K 在 Minecraft 里面建立很多很多的农场,总共 n 个,以至于他自己都忘记了每个 农场中种植作物的具体数量了,他只记得一些含糊的信息(共 m 个),以下列三种形式描 述: 农场 ...

  4. js匹配字符串

    lastIndexOf() 方法可返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索 var str = 'Hello World' str.lastIndexOf('Hell ...

  5. Android为TV端助力 eclipse出现感叹号的解决办法

    当eclipse导入项目出现红叉但无提示错误时,去看:1>菜单路径----Window/Show View/Console2>菜单路径----Window/Show View/Error ...

  6. (后端)mybatis 模糊查询 mapper.xml的写法(转)

    原文地址:https://blog.csdn.net/sc6231565/article/details/46412765 1. sql中字符串拼接 SELECT * FROM tableName W ...

  7. oracle测试环境表空间清理

    测试场景下,使用的oralce遇到表空间的占用超大,可以采用如下的方式进行空间的清理 首先使用sqlplus连接数据库sqlplus sys/password@orcl as sysdba 之类进行数 ...

  8. 服务器CPU繁忙或内存压力引起网络掉包的浅析与总结

      最近一段时间遇到了两起有意思的故障,现象都是网络掉包或网络断开,不过这些只是表面现象,引起现象出现的本质才是我们需要关注的重点: 案例1: 平台   :VMware平台 操作系统 :Windows ...

  9. mysql----JOIN Quiz

    JOIN quiz game id mdate stadium team1 team2 1001 8 June 2012 National Stadium, Warsaw POL GRE 1002 8 ...

  10. 批量配置SSH互信脚本

    在大规模自动化部署时我们常常需要配置好服务器的SSH互信,以便自动化脚本可以免密登录远程服务器,常规的手动配置SSH互信步骤如下: 使用ssh-keygen生成本地ssh key(mha01),生成的 ...