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. springMVC_02hello案例

    1.导入jar包 commons-logging-1.1.1.jar jackson-annotations-2.5.4.jar jackson-core-2.5.4.jar jackson-data ...

  2. Add Again(重复元素排序) UVA11076

    Add Again Summation of sequence of integers is always a common problem in Computer Science. Rather t ...

  3. DNS到底是干什么用的

    DNS,DomainNameSystem或者DomainNameService(域名系统或者余名服务).域名系统为Internet上的主机分配域名地址和IP地址.用户使用域名地址,该系统就会自动把域名 ...

  4. webpack4 系列教程(十三):自动生成HTML文件

    作者按:因为教程所示图片使用的是 github 仓库图片,网速过慢的朋友请移步<webpack4 系列教程(十三):自动生成 HTML 文件>原文地址.更欢迎来我的小站看更多原创内容:go ...

  5. Android Service解析

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

  6. 初学HTML-1

    HTML:Hyper Text Markup Language的缩写    超文本标记语言,用来描述文本的语义,这些文本———超文本,也叫标签. 基本格式: <html> <head ...

  7. POJ1743 Musical Theme(后缀数组 二分)

    Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 33462   Accepted: 11124 Description A m ...

  8. 洛谷P4103 [HEOI2014]大工程(虚树 树形dp)

    题意 链接 Sol 虚树. 首先建出虚树,然后直接树形dp就行了. 最大最小值直接维护子树内到该节点的最大值,然后合并两棵子树的时候更新一下答案. 任意两点的路径和可以考虑每条边两边的贡献,\(d[x ...

  9. 如何用ABP框架快速完成项目(9) - 用ABP一个人快速完成项目(5) - 不要执着于设计模式和DDD理论,避免原教旨主义

    为什么要写这节文章呢?   首先主动看我这系列文章和参加活动课程的同学, 肯定是积极好学的. 所以很大概率是学过设计模式和DDD理论的. 很大概率不是走一点都不懂设计模式和DDD理论这个极端, 而是走 ...

  10. Salesforce 数据备份和恢复小知识

    数据备份的类型 在Salesforce中可以使用多种API进行数据备份,它们是: REST API SOAP API Buik API Metadata API 数据备份有三种选择: 完全备份(Ful ...