Codeforces Gym 100203E E - bits-Equalizer 贪心
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 贪心的更多相关文章
- Codeforces Gym 100203E bits-Equalizer 贪心
原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 考虑到交换可以减少一次操作,那么可以 ...
- codeforces gym #102082C Emergency Evacuation(贪心Orz)
题目链接: https://codeforces.com/gym/102082 题意: 在一个客车里面有$r$排座位,每排座位有$2s$个座位,中间一条走廊 有$p$个人在车内,求出所有人走出客车的最 ...
- Codeforces Gym 100269E Energy Tycoon 贪心
题目链接:http://codeforces.com/gym/100269/attachments 题意: 有长度为n个格子,你有两种操作,1是放一个长度为1的东西上去,2是放一个长度为2的东西上去 ...
- Codeforces GYM 100876 J - Buying roads 题解
Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...
- Codeforces Gym 101252D&&floyd判圈算法学习笔记
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
- Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
- 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 ...
- CodeForces Gym 100213F Counterfeit Money
CodeForces Gym题目页面传送门 有\(1\)个\(n1\times m1\)的字符矩阵\(a\)和\(1\)个\(n2\times m2\)的字符矩阵\(b\),求\(a,b\)的最大公共 ...
- uva12545 Bits Equalizer
uva12545 Bits Equalizer You are given two non-empty strings S and T of equal lengths. S contains the ...
随机推荐
- 【MySQL for Mac】在Mac终端导入&导出.sql文件
导入 打开终端输入:(前提是已经配置过MySQL环境变量) mysql -u root -p create database name; use name; source 『将.sql文件直接拖拽至终 ...
- 【二叉树、堆】15轻院校赛-J-堆
原题:http://acm.zzuli.edu.cn/problem.php?cid=1099&pid=9 [描述] [输入] [输出] Sample Input 3 1 10 3 10 5 ...
- mybatis+spring+struts2框架整合
近期公司要开发新的项目,要用struts2+mybatis+spring框架,所以学习了下,来自己的博客发表下,希望能给大家带来帮助!下边我把我的myschool开发的源代码以及数据库贴出来! 开 ...
- Zend Framework 入门(1)—快速上手
1. 安装 从 Zend Framework 的网页上下载最新版本.解压后,把整个目录拷贝到一个理想的地方,比如:/php/library/Zend. 打开 php.ini 文件,确认包含 Zend ...
- java解析XML四种方法
XML现在已经成为一种通用的数据交换格式,平台的无关性使得很多场合都需要用到XML. XML现在已经成为一种通用的数据交换格式,它的平台无关性,语言无关性,系统无关性,给数据集成与交互带来了极大的方便 ...
- Hadoop Configuration
Configuration的主要是加载配置文件,并储存在properties中. 细节内容不重复了,主要参考Hadoop技术内幕,Hadoop源代码,以及: http://blog.csdn.net/ ...
- IOS AsyncSocket
导入AsyncSocket.h AsyncSocket.m AsyncUdpSocket.h AsyncUdpSocket.m 以及 CFNetWork.framework async ...
- NIS Edit&Nsis打包程序发布(安装和卸载)
转自:http://blog.csdn.net/signjing/article/details/7855855 注意:首选得明确自己需要打包的程序,以及程序需要的dll文件,资源文件等. 1.下载N ...
- [原创]个人工具 - YE快速复制助手(YeFastcopyHelper)
版本:v1.3.216 更新时间:2014/02/16 * 代码完善 + 右键关于显示当前版本号,点击并链接到软件帮助页 Technorati 标签: NET,.NET 3.5,asion C#,Ch ...
- C++实现网格水印之调试笔记(五)—— 提取出错
在实现提取水印的过程中,遇到了一些问题 首先还是根据论文中的思路来梳理一下整个提取流程 读入两个模型,一个原始模型ori_mesh, 一个水印模型wm_mesh. 将两个模型对齐(即放在同一个坐标系下 ...