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. 移动设备3G网站制作的detail

    说明一下,在此所说的移动设备前端开发是指针对高端智能手机(如Iphone.Android),所以需要对webkit内核的浏览器有一定的了解. 1.webkit内核中的一些私有的meta标签 <m ...

  2. Solr与Mysql简单集成

    Solr与Mysql数据库的集成,实现全量索引.增量索引的创建. 基本原理很简单:在Solr项目中注册solr的DataImportHandler并配置Mysql数据源以及数据查询sql语句.当我们通 ...

  3. Android-onTouchEvent方法的使用

    手机屏幕事件的处理方法onTouchEvent.该方法在View类中的定义,并且所有的View子类全部重写了该方法,应用程序可以通过该方法处理手机屏幕的触摸事件.该方法的签名如下所示. public ...

  4. [FIX BUG]获取theme中自定义textColor时报的错误

    我在Fragment中inflate它都可以,可是一旦使用ListView来inflate就会报错,说找不到我自定义的attr!研究了半天发现是我的inflate的context有问题: view = ...

  5. linux中waitpid及wait的用法

    wait(等待子进程中断或结束) 表头文件      #include<sys/types.h>      #include<sys/wait.h> 定义函数 pid_t wa ...

  6. Spring Bean基本管理--bean注入方式汇总

    依赖注入方式:Spring支持两种依赖注入方式,分别是属性注入和构造函数注入.还有工厂方法注入方式. 依赖注入还分为:注入依赖对象可以采用手工装配或自动装配,在实际应用开发中建议使用手工装配,因为自动 ...

  7. 【原】Storm序列化

    5. Storm高级篇 序列化 Dynamic typing Custom serialization Java serialization Component-specific serializat ...

  8. dwr消息推送

    闲来无事,把自己关于对dwr消息推送的实现过程描述一番. 首先第一步,当然在工程中是加入dwr.jar了,接着在web.xml中配置以下代码 <servlet> <servlet-n ...

  9. C++ 我想这样用(五)

    上一篇说了[C with Class]语法的第一部分,下面继续第二部分: 第二部分:面向过程的编程风格 什么是面向过程我想如果你还不知道,那你绝对不是C程序员!其实我个人感觉面向过程.模块式的C编程风 ...

  10. 转-CMMI在中国之混乱-CMMI比ISO9000会更惨

    CMMI在中国之混乱-CMMI比ISO9000会更惨 自己接触CMM/CMMI已经有8年时间了,现在静心回顾一下,觉得CMMI在中国的命运会比ISO9000还悲惨. 一组现象或许让你我对此结论有更深入 ...