uva12545 Bits Equalizer

You are given two non-empty strings S and T of equal lengths. S contains the characters `0', `1' and `?', whereas T contains `0' and `1' only. Your task is to convert S into T in minimum number of moves. In each move, you can

  1. change a `0' in S to `1'
  2. change a `?' in S to `0' or `1'
  3. swap any two characters in S

As an example, suppose S = "01??00" and T = "001010". We can transform S into T in 3 moves:

  • Initially S = "01??00"
  • - Move 1: change S[2] to `1'. S becomes "011?00"
  • - Move 2: change S[3] to `0'. S becomes "011000"
  • - Move 3: swap S[1] with S[4]. S becomes "001010"
  • S is now equal to T

Input

The first line of input is an integer C (C200) 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
000000

Sample Output

Case 1: 3
Case 2: 1
Case 3: -1

这个题目用到的是统计的思想,不用真正的去变字符串里字符的位置,只需要统计哪几个要变动就OK。

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = ;
int num[];
char S[MAXN], T[MAXN];
int solve(int len)
{
memset(num, , sizeof(num));
for(int i = ; i < len; ++i)
{
if(S[i] == '' && T[i] == '' ) num[]++;
else if(S[i] == '' && T[i] == '') num[]++;
else if(S[i] == '?' && T[i] == '') num[]++;
else if(S[i] == '?' && T[i] == '') num[]++;
} int ans = min(num[], num[]);
if(num[] >= num[])
{
num[] -= ans;
if(num[] - num[] >= )
{
ans += *num[];
ans += num[] + num[] - num[];
}
else
{
ans += *num[];
ans += num[] + num[] - num[];
}
}
else if(num[] < num[])
{
num[] -= ans;
if(num[] - num[] >= )
{
ans += *num[];
ans += num[] + num[] - num[];
}
}
return ans;
}
int main()
{
int Tcase, ans;
int tag1, tag2; scanf("%d%*c", &Tcase);
for(int t = ; t <= Tcase; ++t)
{
tag1 = , tag2 = ;
ans = ;
gets(S);
gets(T);
int len = strlen(S); for(int i = ; i < len; ++i)
{
if(S[i] == '') tag1++;
if(T[i] == '') tag2++;
}
if(tag1 > tag2 )
printf("Case %d: %d\n", t, -);
else
{
printf("Case %d: %d\n", t, solve(len));
}
}
return ;
}

uva12545 Bits Equalizer的更多相关文章

  1. 8-3 Bits Equalizer uva12545

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

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

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

  3. UVA 12545 Bits Equalizer

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

  4. UVa 12545 Bits Equalizer (贪心)

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

  5. Bits Equalizer UVA - 12545

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

  6. UVa 12545 Bits Equalizer【贪心】

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

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

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

  8. UVA12545-Bits Equalizer(思维)

    Problem UVA12545-Bits Equalizer Accept: 821  Submit: 4548Time Limit: 3000 mSec Problem Description I ...

  9. [LeetCode] Number of 1 Bits 位1的个数

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...

随机推荐

  1. gzip的使用

    经常会有文件过大,给文件的传输和增添了很多的麻烦,早先得知apach有个base64貌似可以用来压缩文件,但是测试没有什么效果,反而增大了文件的大小.今天了解了java自带的gzip包,如获至宝,超级 ...

  2. [Python] 目录和文件操作

    在Linux系统下用Python写脚本,肯定不能避免各种与目录和文件夹有关的操作.为了以后方便查阅,简单地针对Python中与目录和文件夹有关的操作进行汇总. 需要实现导入的模块为: import o ...

  3. PHP求余函数fmod()

    定义和用法 fmod() 函数返回除法的浮点数余数. 语法 fmod(x,y) 参数 描述 x 必需.一个数. y 必需.一个数. 说明 返回被除数(x)除以除数(y)所得的浮点数余数.余数(r)的定 ...

  4. idea之internal java compiler error

    启动错误:Error:java: Compilation failed: internal java compiler error 解决:将圈选地方改为对应的jdk版本即可

  5. C和指针 第七章 函数递归与迭代

    C语言通过运行时堆栈支持递归函数的实现,递归函数时直接或者间接调用自身的函数,经常有人拿斐波那契实现当做递归的实现,然后这样做效率并不高. n < 1;  Fib(1) =1 n = 2;  F ...

  6. 【转】MVC中处理Json和JS中处理Json对象

    事实上,MVC中已经很好的封装了Json,让我们很方便的进行操作,而不像JS中那么复杂了. MVC中: public JsonResult Test() { JsonResult json = new ...

  7. 使用iText对pdf做权限的操作(不允许修改,不允许复制,不允许另存为),并且加水印等

    添加水印,并且增加权限 @Test public void addWaterMark() throws Exception{ String srcFile="D:\\work\\pdf\\w ...

  8. Cookie使用时需要注意个数及大小限制

    各浏览器对Cookie有一定的限制,在使用时需要格外注意. 各浏览器之间对cookie的不同限制:   IE6.0 IE7.0/8.0/9.0+ Opera FF Safari Chrome cook ...

  9. DOM对象

    DOM是以层次结构组织的节点或信息片断的集合.文档对象模型(Document Object Model)是给HTML与XML文件使用的一组API.DOM的本质是建立网页与脚本语言或程序语言沟通的桥梁. ...

  10. C#静态常量和动态常量的区别

    C#拥有两种不同的常量:静态常量(compile-time constants)和动态常量(runtime  constants).它们有不同的特性,错误的使用不仅会损失效率,还可能造成错误.相比之下 ...