UVa 12545 Bits Equalizer——贪心
来源:互联网 发布:喜欢少年感的男生 知乎 编辑:程序博客网 时间:2024/05/16 09:47
先把能交换的交换了,尽量交换,然后进行数字的变换,变换过程中遇到1 到 0直接输出-1;
变换过程好说,交换过程要考虑的情况比较多
首先把具体的数字进行交换,二层循环
然后把带问号的数字进行交换,具体怎么交换看代码,又是二层循环
最后一层循环解决变换问题
最终复杂度为O(n^2)
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;char a[200], b[200];int n, ans;int main(){ int T, flag = 0; scanf("%d", &T); getchar(); while (T--) { gets(a); gets(b); n = strlen(a); ans = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (j == i) continue; if (a[i] == b[j] && b[i] == a[j] && a[i] != b[i]) { swap(a[i], a[j]); ans++; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (j == i) continue; if (a[i] == '1' && b[i] == '0' && b[j] == '1' && a[j] != '1') { swap(a[i], a[j]); ans++; } else if (a[i] == '0' && b[i] == '1' && a[j] == '?' && b[j] == '0') { swap(a[i], a[j]); ans++; } } } bool ok = true; for (int i = 0; i < n; i++) { if (a[i] == '1' && b[i] == '0') { ok = false; break; } if (a[i] == '?') { a[i] = b[i]; ans++; } else if (a[i] == '0' && b[i] == '1') { a[i] = b[i]; ans++; } } if (!ok) ans = -1; printf("Case %d: %d\n", ++flag, ans); } return 0;}
阅读全文
1 0
- 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(贪心)
- UVA - 12545 Bits Equalizer (贪心)
- UVA 12545 Bits Equalizer(瞎搞 贪心)
- 12545-Bits Equalizer贪心
- 12545 - Bits Equalizer(贪心?)
- Uva - 12545 - Bits Equalizer
- uva 12545Bits Equalizer
- uva 12545 - Bits Equalizer
- UVA 12545 - Bits Equalizer
- UVA 12545 Bits Equalizer
- uva 12545 Bits Equalizer (SWERC 2012) 贪心 分析
- 习题8-3 UVA - 12545 Bits Equalizer(贪心)
- C++之精髓——虚函数
- JSP内置对象
- [Leetcode] 186. Reverse Words in a String II 解题报告
- APP 上传遇到的问题
- 修改orcle数据库为mysql数据库时注意事项
- UVa 12545 Bits Equalizer——贪心
- 软件测试方法策略总结
- 036-Java-027
- c++中的const/const_cast
- JAVA并发
- [Hackerrank题目选做] 出租车司机问题 数据结构+树分治
- 利用Java过滤器实现清理浏览器缓存
- Oracle表空间与数据结构
- Android7.0中文文档(API) -- TimePicker