UVA12545 Bits Equalizer(脑洞)
2015-09-08 17:50
211 查看
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 ofmoves. 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 caseconsists of two lines. The first line is the string S consisting of ‘0’, ‘1’ and ‘?’. The second line is
thestring 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 toconvert 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
给出两个字符串,问你最小变换次数使得两字符串相同。合理的变换有:0变为1,?变为0或1,交换两个相邻字符。
统计题目给出的符合合理变换的数目,包括num1计数"?1",num0计数"?0" ,zero计数 "01" ,one计数"10"。
如果"01"数目加上"?1"数目少于"10"数目,则不能通过交换使两个字符串相同。
如果"01"数目大于等于"10"数目则变换次数为zero + num0 + num1。
否则变换次数为one + num0 + num1。
AC代码:
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 caseconsists of two lines. The first line is the string S consisting of ‘0’, ‘1’ and ‘?’. The second line is
thestring 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 toconvert 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
给出两个字符串,问你最小变换次数使得两字符串相同。合理的变换有:0变为1,?变为0或1,交换两个相邻字符。
统计题目给出的符合合理变换的数目,包括num1计数"?1",num0计数"?0" ,zero计数 "01" ,one计数"10"。
如果"01"数目加上"?1"数目少于"10"数目,则不能通过交换使两个字符串相同。
如果"01"数目大于等于"10"数目则变换次数为zero + num0 + num1。
否则变换次数为one + num0 + num1。
AC代码:
#include "iostream" #include "cstdio" #include "cstring" #include "algorithm" using namespace std; const int MAXN = 105; int main(int argc, char const *argv[]) { int t; scanf("%d", &t); for(int cas = 1; cas <= t; ++cas) { char s1[MAXN], s2[MAXN]; scanf("%s%s", s1, s2); int len1 = strlen(s1), len2 = strlen(s2); if(len1 != len2) { printf("Case %d: -1\n", cas); continue; } int zero = 0, one = 0, num1 = 0, num0 = 0; for(int i = 0; i < len1; ++i) { if(s1[i] == '?') { if(s2[i] == '1') num1++; if(s2[i] == '0') num0++; } if(s1[i] == '0' && s2[i] == '1') zero++; if(s1[i] == '1' && s2[i] == '0') one++; } if(zero >= one) printf("Case %d: %d\n", cas, zero + num0 + num1); else if(zero + num1 < one) printf("Case %d: -1\n", cas); // 无法通过交换满足题意 else printf("Case %d: %d\n", cas, one + num1 + num0); } return 0; }
相关文章推荐
- 【前端学习笔记】2015-09-08~~~~ 关于切图的简单方法
- Userdefault在Mac下Xcode中的存储位置
- OpenCVz中的几种角点检测算法
- 使用tabindex属性是的标签具有onblur,onfocus方法
- 第三方平台支付清算之---对账清算slq脚本
- 从零开始编写图片加载库(二)之AsyncTask图片下载
- 通过rpm方式安装ipsvadm
- mac中myeclipse10连接hadoop-2.6.0集群开发环境问题汇总
- 为UIWebView实现离线浏览
- [LeedCode OJ]#86 Partition List
- javascript新手入门必读书籍推荐
- UVA 11235 Frequent values (RMQ )
- IOS 视图控制对象生命周期-init、viewDidLoad、viewWillAppear、viewDidAppear、viewWillDisappear等的区别及用途
- tcpdump查看某个端口数据
- 从零开始编写图片加载库(一)之Handler+Thread图片下载
- Android: Looper, Handler, HandlerThread. Part I
- 【剑指offer】面试题3:二维数组的查找
- c++ int转换为枚举类型
- 重写(overwrite)与重载(overload)的区别
- Swift的基本使用(2)函数的定义