uva12545 Bits Equalizer
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
- change a `0' in S to `1'
- change a `?' in S to `0' or `1'
- 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 (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
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的更多相关文章
- 8-3 Bits Equalizer uva12545
题意: 给出字符串s包含'0' '1' '?'; 再给出字符串t只包含01: 现在我们可以对S做三个操作:把0变成1,把?变成0或1,任意两个位置交换: 问最少操作几次s == t: 贪心 默认除去那 ...
- 【习题 8-3 UVA - 12545】Bits Equalizer
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 如果1的个数第一个串比第2个串多. 那么就无解. 否则. 找几个位置去凑1 优先找'?'然后才是0的位置 剩余的全都用swap操作就 ...
- UVA 12545 Bits Equalizer
题意: 两个等长的字符串p和q,p有‘0’,‘1’,‘?’组成,q由‘0’,‘1’组成.有三种操作:1.将‘?’变成0:2.将‘?’变成‘1’:3.交换同一字符串任意两个位置上的字符.问有p变到q最少 ...
- UVa 12545 Bits Equalizer (贪心)
题意:给出两个等长的字符串,0可以变成1,?可以变成0和1,可以任意交换s中任意两个字符的位置,问从s变成t至少需要多少次操作. 析:先说我的思路,我看到这应该是贪心,首先,如果先判断s能不能变成t, ...
- Bits Equalizer UVA - 12545
点击打开链接 #include<cstdio> #include<cstring> /* 别看错了:0能变1,1不能变0 能完成的条件是,s与t长度相等且s中0数量和?数量之和 ...
- UVa 12545 Bits Equalizer【贪心】
题意:给出两个等长的字符串,0可以变成1,?可以变成0和1,可以任意交换s中任意两个字符的位置,问从s变成t至少需要多少次操作 先可以画个草图 发现需要考虑的就是 1---0 0---1 ?---0 ...
- UVA - 12545 Bits Equalizer (比特变换器)(贪心)
题意:输入两个等长(长度不超过100)的串S和T,其中S包含字符0,1,?,但T只包含0和1,你的任务是用尽量少的步数把S变成T.有以下3种操作: 1.把S中的0变成1. 2.把S中的“?”变成0或1 ...
- UVA12545-Bits Equalizer(思维)
Problem UVA12545-Bits Equalizer Accept: 821 Submit: 4548Time Limit: 3000 mSec Problem Description I ...
- [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 ...
随机推荐
- entity1
- log4j日志工具
一.关于日志 1.日志定义: 项目在运行阶段产生的信息 2.日志级别 最常见的日志级别有4个: error :错误日志 warn:警告日志 info:流程日志 debug:调试日志 优先级从高到低 ...
- ExtJs 实现表单联动
最近做的项目使用Extjs.遇到表单联动的业务.下面来说说主要实现思想: 说明:表单联动一般存在从属关系,有大范围的对象和大范围中的小对象.比如地理位置的选定(例:浙江省-杭州市-某某县).在这里,我 ...
- JAVA关键字与保留字说明及使用
1.abstract 2.boolean 3.break 4.byte 5.case 6.catch 7.char 8.class 9.continue 10.default 11.do 12.dou ...
- Linux/CentOS 搭建 SVN 项目
1.安装svn yum -y install subversion 2.创建svn仓库路径 mkdir -p /opt/svn/project1 mkdir -p /opt/svn ...
- Hadoop家族的各个成员
官方定义:hadoop是一个开发和运行处理大规模数据的软件平台.核心词语是平台,也就是说我们有大量的数据,又有好几个电脑,我们知道应该把处理数据的任务分解到各个电脑上,但是不知道怎样分配任务,怎样回收 ...
- samtools常用命令详解
samtools的说明文档:http://samtools.sourceforge.net/samtools.shtmlsamtools是一个用于操作sam和bam文件的工具合集.包含有许多命令.以下 ...
- python pickle
>>> import pickle >>> m_list=[',2,'asa'] >>> m_list [', 2, 'asa'] >> ...
- JavaScript "自"运行-setInertval()和setTimeout()理解
setInterval()和clearInterval() var result = "Y"; function onOk() { var tid = setInterval(fu ...
- GitHub for windows呆瓜级入门
一.GitHub是一个远程数据托管平台,对于代码用于版本控制(保存各个阶段的代码版本).首先去 https://github.com/ 注册一个GitHub账号 二.输入用户名(不能重复,相当于在Gi ...