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. R语言读取本地文件注意事项

    R里面应该用/,而不是\ ,或者用两个\\   R区分大小写,所以应该用C:,而不是c:

  2. 在linux终端远程登陆linux服务器

    在linux终端远程登陆linux服务器   原来在Linux终端远程登陆linux服务器是那么的容易,如果的服务器用户名是abc(也可以是root),只需要在终端输入: 然后电脑会提示输入密码就登录 ...

  3. html5 新选择器 querySelector querySelectorAll

    querySelector 返回满足条件的单个元素 使用实例 HTML <div id="main">主体布局</div> JS var main =doc ...

  4. centos6.5安装oracle11g_2

    centos7安装oracle数据库不成功,换成centos6.5安装,可以安装成功,记录一下 安装系统时,主机名如果不是用localhost,安装成功后,要用主机名和ip做映射,修改/etc/hos ...

  5. Path Sum

    需如下树节点求和 5  /  \ 4     8  /     /  \ 11  13    4 / \     /  \  7    2      5   1 JavaScript实现 window ...

  6. css之图片路径

    关于背景图片url路径:图片和调用文件在两个不同根下的(不在同一个包(文件夹)中),要用相对路径,举例"../images/cq.gif":图片和调用文件是在一个根下的直接孩子,用 ...

  7. 短信接口API

    /** * Created by bingone on 15/12/16. */ import org.apache.http.HttpEntity; import org.apache.http.N ...

  8. Bash 中同名的内部命令和外部命令

    昨天有个人在 bug-bash 上问:为什么 [ --help 没有输出帮助信息.有人回答他了,原因是 coreutils 提供的 [ 命令才接受 --help 选项,Bash 自己的 [ 命令不接受 ...

  9. PHP图像裁剪为任意大小的图像,图像不变形,不留下空白

    <?php /** * 说明:函数功能是把一个图像裁剪为任意大小的图像,图像不变形 * 参数说明:输入 需要处理图片的 文件名,生成新图片的保存文件名,生成新图片的宽,生成新图片的高 */ fu ...

  10. python 字典的函数

    clear(),清空 注意单纯的赋值就相当于c语言中引用,只事额外起了一个别名,所以他们指向相同的地址, 所以令c={},只是另外开辟了一个新的空间让c为空,并没有改变之前的空间,所以{}与clear ...