【链接】 我是链接,点我呀:)

【题意】

在这里输入题意

【题解】

如果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的更多相关文章

  1. UVA 12545 Bits Equalizer

    题意: 两个等长的字符串p和q,p有‘0’,‘1’,‘?’组成,q由‘0’,‘1’组成.有三种操作:1.将‘?’变成0:2.将‘?’变成‘1’:3.交换同一字符串任意两个位置上的字符.问有p变到q最少 ...

  2. UVa 12545 Bits Equalizer (贪心)

    题意:给出两个等长的字符串,0可以变成1,?可以变成0和1,可以任意交换s中任意两个字符的位置,问从s变成t至少需要多少次操作. 析:先说我的思路,我看到这应该是贪心,首先,如果先判断s能不能变成t, ...

  3. UVa 12545 Bits Equalizer【贪心】

    题意:给出两个等长的字符串,0可以变成1,?可以变成0和1,可以任意交换s中任意两个字符的位置,问从s变成t至少需要多少次操作 先可以画个草图 发现需要考虑的就是 1---0 0---1 ?---0 ...

  4. UVA - 12545 Bits Equalizer (比特变换器)(贪心)

    题意:输入两个等长(长度不超过100)的串S和T,其中S包含字符0,1,?,但T只包含0和1,你的任务是用尽量少的步数把S变成T.有以下3种操作: 1.把S中的0变成1. 2.把S中的“?”变成0或1 ...

  5. uva12545 Bits Equalizer

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

  6. Bits Equalizer UVA - 12545

    点击打开链接 #include<cstdio> #include<cstring> /* 别看错了:0能变1,1不能变0 能完成的条件是,s与t长度相等且s中0数量和?数量之和 ...

  7. 紫书 习题8-3 UVa 12545 (构造法)

    参考了https://blog.csdn.net/catglory/article/details/47188949 最后推出来操作的个数为问号的个数 加上 同一位置上S串为0而T串为1的位置数量 与 ...

  8. UVA 11645 - Bits(数论+计数问题)

    题目链接:11645 - Bits 题意:给定一个数字n.要求0-n的二进制形式下,连续11的个数. 思路:和 UVA 11038 这题相似,枚举中间,然后处理两边的情况. 只是本题最大的答案会超过l ...

  9. 8-3 Bits Equalizer uva12545

    题意: 给出字符串s包含'0' '1' '?'; 再给出字符串t只包含01: 现在我们可以对S做三个操作:把0变成1,把?变成0或1,任意两个位置交换: 问最少操作几次s == t: 贪心 默认除去那 ...

随机推荐

  1. java解析json文件(省,市,区)

    [{"code":"11","name":"北京市"},{"code":"12" ...

  2. Delayer 基于 Redis 的延迟消息队列中间件

    Delayer 基于 Redis 的延迟消息队列中间件,采用 Golang 开发,支持 PHP.Golang 等多种语言客户端. 参考 有赞延迟队列设计 中的部分设计,优化后实现. 项目链接:http ...

  3. 紫书 例题 10-5 UVa 12716 (枚举方式)

    设gcd(a, b) = a xor b = c 那么我们可以证明c=a-b 那么同时c又是a的因子(c是a与b的最大公因数) 所以我们可以枚举c,然后枚举c的倍数,也就是a 有了a和c可以算出b为a ...

  4. [洛谷P3121] 审查(黄金) (AC自动机)

    题目描述 FJ把杂志上所有的文章摘抄了下来并把它变成了一个长度不超过10^5的字符串S.他有一个包含n个单词的列表,列表里的n个单词记为t_1...t_N.他希望从S中删除这些单词. FJ每次在S中找 ...

  5. CSU 1364 Interview RMQ

    题意: 瑶瑶有一家有一家公司,最近他想招m个人.因为他的公司是如此的出名,所以有n个人来参加面试.然而,瑶瑶是如此忙,以至于没有时间来亲自面试他们.所以他准备选择m场面试来测试他们. 瑶瑶决定这样来安 ...

  6. 【Henu ACM Round#19 B】 Luxurious Houses

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 从右往左维护最大值. 看到比最大值小(或等于)的话.就递增到比最大值大1就好. [代码] #include <bits/std ...

  7. java-proxool 异常

    使用 proxool,JDBC连接池,进行批量运行的时候遇到异常: The Thread responsible was named 'Thread-32′, but the last SQL it ...

  8. IIS 无法读取配置节&quot;system.web.extensions&quot;,由于它缺少节声明

    作者:jiankunking 出处:http://blog.csdn.net/jiankunking 今天在本地安装iis.搭建站点,应用程序的时候报错以下的错误: server错误 Internet ...

  9. .vscode folder

    https://stackoverflow.com/questions/32964920/should-i-commit-the-vscode-folder-to-source-control Che ...

  10. VMware中CentOS6.5启动出现An error occurred during the file system check