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. site

    http://blog.csdn.net/zgmzyr/article/details/7657126

  2. HDU 5365 Run

    题意:给n个整点,问用其中若干个做顶点能够成多少个正三角形或正四边形或正五边形或正六边形. 解法:出题人说 地球人都知道整点是不能构成正五边形和正三边形和正六边形的,所以只需暴力枚举四个点判断是否是正 ...

  3. 祭奠我的csdn博客

    本人在csdn的博客莫名其妙地被封了(http://blog.csdn.net/fty8788),非常郁闷. 回忆起,可能是我近半年由于工作事情忙很少写博客了,被某213盗用发了不恰当的东东.我也查不 ...

  4. Canvas入门(3):图像处理和绘制文字

    来源:http://www.ido321.com/997.html 一.图像处理(非特别说明,所有结果均来自最新版Google) 在HTML 5中,不仅可以使用Canvas API绘制图形,也可以用于 ...

  5. 线段和矩形相交 POJ 1410

    // 线段和矩形相交 POJ 1410 // #include <bits/stdc++.h> #include <iostream> #include <cstdio& ...

  6. C语言——递归练习

    1.炮弹一样的球状物体,能够堆积成一个金字塔,在顶端有一个炮弹,它坐落在一个4个炮弹组成的层面上,而这4个炮弹又坐落在一个9个炮弹组成的层面上,以此类推.写一个递归函数CannonBall,这个函数把 ...

  7. CTS FAIL(一)

    首先简单介绍下CTS:全称Compatibility Test Suite,通过CTS测试,来检测android apk与android系统的兼容性. 最近公司release一版新的Image,但在新 ...

  8. hbase使用-java操作

      .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courie ...

  9. linux查看端口信息以及关闭进程

     lsof -i:6633 查看端口6633的使用情况 kill  (+PID数值),结束进程

  10. Java集合之ArrayList和LinkedList的实现原理以及Iterator详解

    ArrayList实现可变数组的原理: 当元素超出数组内容,会产生一个新数组,将原来数组的数据复制到新数组中,再将新的元素添加到新数组中. ArrayList:是按照原数组的50%来延长,构造一个初始 ...