Uva - 12545 - Bits Equalizer
来源:互联网 发布:linux 安装oracle12c 编辑:程序博客网 时间:2024/05/16 05:31
刚开始想了一个变换顺序,模拟进行操作,写了浩浩荡荡200多行最后WA了,感觉还是方法不对。。。后来从只能0变1,不能1变0入手,应该先满足上1下0的情况,其他的三种情况用简单的变换就可以了。
先用0-1和1-0进行配对,配对一次,交换S中的0和1。
如果没有0-1了,那就用?-1和1-0进行配对,先把?换成0,再执行上一步。
如果0-1和?-1都没有了,但是1-0还有,说明无解。
把上面的1配对成功之后,其他的都可以通过一次操作配对。
AC代码:
#include <iostream>#include <cstdio>#include <cstdlib>#include <cctype>#include <cstring>#include <string>#include <sstream>#include <vector>#include <set>#include <map>#include <algorithm>#include <stack>#include <queue>#include <bitset> #include <cassert> #include <cmath>#include <functional>using namespace std;const int maxn = 105;int oneVzero, zeroVone, qVone, qVzero;int ans;string S, T;void solve(){for (int i = 0; i < S.length(); i++) {if (S[i] == '1' && T[i] == '0') {oneVzero++;}else if (S[i] == '0' && T[i] == '1') {zeroVone++;}else if (S[i] == '?' && T[i] == '0') {qVzero++;}else if (S[i] == '?' && T[i] == '1') {qVone++;}}while (oneVzero && zeroVone) { // 交换1-0和0-1oneVzero--;zeroVone--;ans++;}while (oneVzero && qVone) { // 交换1-0和?-1oneVzero--;qVone--;ans += 2;}if (oneVzero) {ans = -1;}else {ans += zeroVone + qVone + qVzero;}}int main(){ios::sync_with_stdio(false);int C;cin >> C;int kase = 0;while (C--) {cin >> S >> T;// 初始化ans = 0;oneVzero = zeroVone = qVzero = qVone = 0;solve();cout << "Case " << ++kase << ": " << ans << endl;}return 0;}
0 0
- Uva - 12545 - Bits Equalizer
- uva 12545Bits Equalizer
- uva 12545 - Bits Equalizer
- UVA 12545 - Bits Equalizer
- UVA 12545 Bits Equalizer
- UVA - 12545 Bits Equalizer 贪心
- UVA 12545(p253)----Bits Equalizer
- uva 12545 Bits Equalizer(贪心)
- UVA 12545 Bits Equalizer(模拟+贪心)
- uva 12545 - Bits Equalizer(比特变化器)
- UVa #12545 Bits Equalizer (习题8-3)
- UVa 12545 - Bits Equalizer(贪心)
- UVa:12545 Bits Equalizer(贪心)
- uva 12545——Bits Equalizer
- UVA - 12545 Bits Equalizer (贪心)
- UVA 12545 Bits Equalizer(瞎搞 贪心)
- UVa 12545 Bits Equalizer——贪心
- UVA 12545 Bits Equalizer 机智题
- ASP.NET获取客户端及服务器的信息
- VC中".pch"是什么文件,没有.pch文件怎么办
- 利用gallery在Ubuntu Scope中显示多张图片
- javascript闭包
- 创建回调函数
- Uva - 12545 - Bits Equalizer
- android dialog圆角显示及解决出现的黑色棱角
- poj2923 Relocation
- 虚拟机入门(2)之虚拟机的三种网络模式
- SQLyog v11.24查询MySQL5.6.24中文乱码的解决方法
- Virtual Box 桥接或NAT上网 和 host-only方式
- TCP/IP三次握手及断开四次握手过程
- linux之fstab文件详解
- TextView中的文本实现部分响应点击