比特变换器 Bits Equalizer,SWERC 2012 ,UVA12545
来源:互联网 发布:java中循环遍历数组 编辑:程序博客网 时间:2024/06/14 09:53
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 of
moves. 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 case
consists of two lines. The first line is the string S consisting of ‘0’, ‘1’ and ‘?’. The second line is the
string 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 to
convert 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,而1不能到0,这是一关键点,这个代码只打了一遍,没有做过优化,会有些长,望见谅哈0w0。
#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<iostream>using namespace std;char a[105],b[105],c[105];int main(){ int q; int p=0; cin>>q; while(q--) { memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); p++; int panding=1; int shang=0,sum=0,xia=0, y=0; getchar(); cin>>a; cin>>b; int k=strlen(a); for(int i=0;i<strlen(a);i++) { if(a[i]==b[i]) {k--;c[i]=1;} } for(int i=0;i<strlen(a);i++) { if(c[i]==0) { if((a[i]+b[i]==97)&&(a[i]=='1'))shang++,k--; else if((a[i]+b[i]==97)&&(a[i]=='0'))xia++,k--; else if(a[i]=='?'&&b[i]=='1')y++; } } if(shang>=xia) { int sheng=shang-xia; if(sheng>k){panding=0;} else { if(y>=sheng){sum=xia+k+sheng;} else {panding=0;} } } else { sum=xia+k; } if(panding) printf("Case %d: %d\n",p,sum); else printf("Case %d: -1\n",p); }}
- 比特变换器 Bits Equalizer,SWERC 2012 ,UVA12545
- 习题8-3 比特变换器(Bits Equalizer, SWERC 2012, UVa12545)
- UVA12545:Bits Equalizer(比特变换器)
- 比特变换器 Bits Equalizer
- uva12545比特变换器
- [UVA12545]Bits Equalizer[贪心]
- UVA12545 Bits Equalizer(脑洞)
- UVA12545 Bits Equalizer
- [思路贪心]UVa12545 - Bits Equalizer
- uva 12545 Bits Equalizer (SWERC 2012) 贪心 分析
- uva 12545 - Bits Equalizer(比特变化器)
- 12545-Bits Equalizer贪心
- Uva - 12545 - Bits Equalizer
- 12545 - Bits Equalizer(贪心?)
- uva 12545Bits Equalizer
- uva 12545 - Bits Equalizer
- UVA 12545 - Bits Equalizer
- UVA 12545 Bits Equalizer
- Android系统Intent中的Uri使用
- RMQ算法
- java中静态成员与非静态成员之间的访问关系
- mybatis动态表名遇见There is no getter for property named 'tableName' in 'class java.lang.String'
- yum使用代理的设置
- 比特变换器 Bits Equalizer,SWERC 2012 ,UVA12545
- java基础学习总结——数组
- SQL调优 EXPLAINE
- 用数组模拟链表操作 C实现~
- Qt之界面实现技巧
- 理解RESTful架构
- 新手上路系列10:信号量与信号通信
- NLTK01 《NLTK基础教程--用NLTK和Python库构建机器学习应用》
- Java 内存分配全面浅析