【习题 8-3 UVA - 12545】Bits Equalizer
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
如果1的个数第一个串比第2个串多。
那么就无解。
否则。
找几个位置去凑1
优先找'?'然后才是0的位置
剩余的全都用swap操作就好
【代码】
/*
1.Shoud it use long long ?
2.Have you ever test several sample(at least therr) yourself?
3.Can you promise that the solution is right? At least,the main ideal
4.use the puts("") or putchar() or printf and such things?
5.init the used array or any value?
6.use error MAX_VALUE?
7.use scanf instead of cin/cout?
8.whatch out the detail input require
*/
/*
一定在这里写完思路再敲代码!!!
*/
#include <bits/stdc++.h>
using namespace std;
int T,num[2][2],n;
string s1,s2;
void change(){
for (int i = 0;i < n;i++)
if (s1[i]=='?' && s2[i]=='1'){
s1[i] = '1';
return;
}
for (int i = 0;i < n;i++)
if (s1[i]=='0' && s2[i]=='1'){
s1[i]= '1';
return;
}
for (int i = 0;i < n;i++)
if (s1[i]=='?'){
s1[i] = '1';
return;
}
for (int i = 0;i < n;i++)
if (s1[i]=='0') {
s1[i] = '1';
return;
}
}
int main(){
#ifdef LOCAL_DEFINE
freopen("rush_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(0),cin.tie(0);
cin >> T;
int kase= 0;
while (T--){
memset(num,0,sizeof num);
cin >> s1 >> s2;
n = s1.size();
for (int i = 0;i < n;i++)
if (s1[i]>='0' && s1[i] <='1')
num[0][s1[i]-'0']++;
for (int i = 0;i < n;i++)
if (s2[i]>='0' && s2[i] <='1')
num[1][s2[i]-'0']++;
if (num[0][1]>num[1][1]){
cout <<"Case "<<++kase<<": "<< -1 << endl;
continue;
}
int ans = 0;
while (num[0][1] < num[1][1]){
ans++;
change();
num[0][1]++;
}
for (int i = 0;i < n;i++)
if (s1[i]=='?') s1[i] = '0',ans++;
for (int i = 0;i < n;i++)
if (s1[i]=='1' && s1[i]!=s2[i]){
ans++;
}
cout <<"Case "<<++kase<<": "<< ans << endl;
}
return 0;
}
【习题 8-3 UVA - 12545】Bits Equalizer的更多相关文章
- 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, ...
- 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
uva12545 Bits Equalizer You are given two non-empty strings S and T of equal lengths. S contains the ...
- Bits Equalizer UVA - 12545
点击打开链接 #include<cstdio> #include<cstring> /* 别看错了:0能变1,1不能变0 能完成的条件是,s与t长度相等且s中0数量和?数量之和 ...
- 紫书 习题8-3 UVa 12545 (构造法)
参考了https://blog.csdn.net/catglory/article/details/47188949 最后推出来操作的个数为问号的个数 加上 同一位置上S串为0而T串为1的位置数量 与 ...
- UVA 11645 - Bits(数论+计数问题)
题目链接:11645 - Bits 题意:给定一个数字n.要求0-n的二进制形式下,连续11的个数. 思路:和 UVA 11038 这题相似,枚举中间,然后处理两边的情况. 只是本题最大的答案会超过l ...
- 8-3 Bits Equalizer uva12545
题意: 给出字符串s包含'0' '1' '?'; 再给出字符串t只包含01: 现在我们可以对S做三个操作:把0变成1,把?变成0或1,任意两个位置交换: 问最少操作几次s == t: 贪心 默认除去那 ...
随机推荐
- [JSOI2007]建筑抢修 优先队列 贪心
Code: #include<cstdio> #include<algorithm> #include<cstring> #include<queue> ...
- bzoj5085: 最大 暴力 二分
Code: #include<cstdio> #include<algorithm> #include<cstring> #include<iostream& ...
- ln---创建链接
ln命令用来为文件创件连接,连接类型分为硬连接和符号连接两种,默认的连接类型是硬连接.如果要创建符号连接必须使用"-s"选项. 注意:符号链接文件不是一个独立的文件,它的许多属性依 ...
- UVA10269 Adventure of Super Mario(Floyd+DP)
UVA10269 Adventure of Super Mario(Floyd+DP) After rescuing the beautiful princess, Super Mario needs ...
- 2016届 阿里巴巴校招研发project师C/C++笔试题--2015.08.23
选择题牛客网地址题目1:http://www.nowcoder.com/test/255234/summary. 题目2:http://www.nowcoder.com/test/262758/sum ...
- File的getPath()和getAbsolutePath()和getCanonicalPath()的差别
这几个方法是有一次无意的发现,我当时也不知道什么意思,就百度了,查到了一些列子: 原文地址http://www.blogjava.net/dreamstone/archive/2007/08/08/1 ...
- OpenCASCADE Incremental Mesh
OpenCASCADE Incremental Mesh eryar@163.com Abstract. OpenCASCADE IncrementalMesh is used to build th ...
- Gitblit从一个服务器,迁移到另外一个服务器
http://gitblit.com/federation.html A Gitblit federation is a mechanism to clone repositories and kee ...
- “DNS隧道”盗号木马分析——类似hjack偷密码然后利用dns tunnel直传数据发送出去
摘自:http://www.freebuf.com/articles/network/38276.html# 运行后不断监控顶端窗口,一旦发现为QQ,就弹出一个自己伪造的QQ登陆窗口,诱导用户输入密码 ...
- C++中explicit关键字作用
explicit是c++中不太常用的一个关键字,其作用是用于修饰构造函数,告诉编译器定义对象时不做隐式转换. 举例说明: include <iostream> include <st ...