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 (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的更多相关文章
- 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 ...
随机推荐
- c# winform 动态画矩形 矩形大小可以拖动
http://jhlong12345.blog.163.com/blog/static/1230631292015544450189/# 结合上一篇,继续 矩形大小的调整 还有小bug,思路有了 ...
- %我的 tex 模版
%我的 tex 模版 \documentclass[UTF8,a1paper,landscape]{ctexart}%UTF8 中文支持,a1paper 纸张大小,landscape 横向版面,cte ...
- 线程安全的无锁RingBuffer的实现
这里的线程安全,是指一个读线程和一个写线程,读写两个线程是安全的,而不是说多个读线程和多个写线程是安全的.. 在程序设计中,我们有时会遇到这样的情况,一个线程将数据写到一个buffer中,另外一个线程 ...
- WinPcap4.13无法安装解决方法
360软件管家提示把WinPcap更新至版本:4.1.0.2980,于是把旧版下载后,可新版本怎么也无法顺利安装,出现以下信息,旧版本已安装,关闭所有winpcap-based应用程序和再次运行安装程 ...
- 什么叫session和cookie-及其设置
http的无状态? 保持状态, 是指当程序关闭后重启, 上一次操作的历史还能继续, 保持的. 如word中的 "选项"设置. 如windows系统的设置等等. http的设计目的, ...
- Excel函数——DATE、SUBSTITUTE、REPLACE、ISERROR、IFERROR
1.DATE DATE 函数返回表示特定日期的连续序列号.例如,公式 =DATE(2008,7,8) 返回 2008-7-8或39637,取决于单元格格式,但空单元格计算和默认为日期格式. DATE也 ...
- Hadoop-HBASE案例分析-Hadoop学习笔记<二>
之前有幸在MOOC学院抽中小象学院hadoop体验课. 这是小象学院hadoop2.X概述第八章的笔记 主要介绍HBase,一个分布式数据库的应用案例. 案例概况: 1)时间序列数据库(OpenTSD ...
- SpringMVC Controller介绍
SpringMVC Controller 介绍 一.简介 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理 ...
- tornado 重定向404(方法不对)
application = tornado.web.Application(url_wrapper([ (r"", include('app.views.web_services. ...
- 日志解析LogParse启动参数配置
-task task_stat1001to1010.yaml -log log4j_stat1001to1010.xml 用绝对路径