E - bits-Equalizer
Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87794#problem/K

Description

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 (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

HINT

题意

给你一个s1和s2,你每次有三种操作,第一种是将0变成1,第二种是把问号变成1或者0,第三种是交换任意两个字符的位置

题解

贪心,首先处理问号,然后再处理0变成1的问题,最后处理交换位置的问题

每次处理都直接扫一遍就好了

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> using namespace std; string s1,s2;
int num1x,num1y,num2x,num2y;
int main()
{
int t;
scanf("%d",&t);
for(int cas=;cas<=t;cas++)
{
num1x=num1y=num2x=num2y=;
cin>>s2>>s1;
int len=s1.size();
for(int i=;i<len;i++)
{
if(s1[i]=='')
num1x++;
if(s1[i]=='')
num1y++;
if(s2[i]=='')
num2x++;
if(s2[i]=='')
num2y++;
}
int flag=;
int ans=;
for(int i=;i<len;i++)
{
if(num2x>num1x)
{
flag=;
break;
}
if(s2[i]=='?')
{
if(s1[i]=='')
{
if(num2x<num1x)
{
s2[i]='';
num2x++;
ans++;
}
else
{
s2[i]='';
num2y++;
ans++;
}
}
else
{
if(num2y<num1y)
{
s2[i]='';
num2y++;
ans++;
}
else
{
s2[i]='';
num2x++;
ans++;
}
}
}
}
if(flag==)
{
printf("Case %d: -1\n",cas);
continue;
}
for(int i=;i<len;i++)
{
if(num1x==num2x&&num2x==num2y)
break;
if(s2[i]==''&&s1[i]=='')
{
if(num1x>num2x)
{
num2y--;
num2x++;
s2[i]='';
ans++;
}
}
}
int tmp=;
for(int i=;i<len;i++)
{
if(s1[i]!=s2[i])
{
tmp++;
}
}
printf("Case %d: %d\n",cas,ans+tmp/);
}
}

Codeforces Gym 100203E E - bits-Equalizer 贪心的更多相关文章

  1. Codeforces Gym 100203E bits-Equalizer 贪心

    原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 考虑到交换可以减少一次操作,那么可以 ...

  2. codeforces gym #102082C Emergency Evacuation(贪心Orz)

    题目链接: https://codeforces.com/gym/102082 题意: 在一个客车里面有$r$排座位,每排座位有$2s$个座位,中间一条走廊 有$p$个人在车内,求出所有人走出客车的最 ...

  3. Codeforces Gym 100269E Energy Tycoon 贪心

    题目链接:http://codeforces.com/gym/100269/attachments 题意: 有长度为n个格子,你有两种操作,1是放一个长度为1的东西上去,2是放一个长度为2的东西上去 ...

  4. Codeforces GYM 100876 J - Buying roads 题解

    Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...

  5. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

  6. Codeforces Round #546 (Div. 2) D 贪心 + 思维

    https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...

  7. Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】

     2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...

  8. CodeForces Gym 100213F Counterfeit Money

    CodeForces Gym题目页面传送门 有\(1\)个\(n1\times m1\)的字符矩阵\(a\)和\(1\)个\(n2\times m2\)的字符矩阵\(b\),求\(a,b\)的最大公共 ...

  9. uva12545 Bits Equalizer

    uva12545 Bits Equalizer You are given two non-empty strings S and T of equal lengths. S contains the ...

随机推荐

  1. 【转】出現 "PowerCam player support IE browser only!" 的錯誤訊息

    原文网址:http://www.camdemy.com/faq/1138 A.  這是由於新版 IE11 針對文件模式設定的改變,衍生 PowerCam5 及6 的教材閱讀問題 ( EverCam 已 ...

  2. HDU 1387 Team Queue

    Team Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  3. (原创)LAMP教程4-用VirtualBox安装64位的centos6.4

    (原创)LAMP教程4-用VirtualBox安装64位的centos6.4 好的,今天就要开始正式的讲一些有营养的东西了,是的,没有错就是讲如何用VirtualBox安装64位的centos6.4 ...

  4. HDU-4882 ZCC Loves Codefires

    http://acm.hdu.edu.cn/showproblem.php?pid=4882 ZCC Loves Codefires Time Limit: 2000/1000 MS (Java/Ot ...

  5. 白盒测试之gtest第一个demo

    认识gtest工具后,关于它的使用,下面将用一个demo程序演示一下gtest的用法以及成果展示. 一.需要测试的C++代码: #include "myfunction.h" // ...

  6. SQL你必须知道的-查询聚合分组排序

    use MySchoolTwo    -- 简单查询    select * from Student    -- 话说这种查询的效率要比 * 要高级点    select sId , sName , ...

  7. Flume OG 与 Flume NG 的区别

    1.Flume OG:Flume original generation 即Flume 0.9.x版本    Flume NG:Flume next generation ,即Flume 1.x版本 ...

  8. [转]使用CSS3 Grid布局实现内容优先

    使用CSS3 Grid布局实现内容优先  http://www.w3cplus.com/css3/css3-grid-layout-module.html 本文由大漠根据Rachel Andrew的& ...

  9. Python学习笔记——正则表达式

    今天把之前学的正则表达式好好总结总结. 一.元字符 . :  .表示可以匹配任意一个字符 \d  :  \d表示可以匹配任意一个数字 \D  : \D表示可以匹配任意一个非数字 \s  :  \s表示 ...

  10. Hbase笔记——RowKey设计

    一).什么情况下使用Hbase 1)传统数据库无法承载高速插入.大量读取. 2)Hbase适合海量,但同时也是简单的操作. 3)成熟的数据分析主题,查询模式确立不轻易改变. 二).现实场景 1.电商浏 ...