UVA 12545 Bits Equalizer
来源:互联网 发布:js验证提示信息 编辑:程序博客网 时间:2024/05/16 08:26
Bits Equalizer
64-bit integer IO format: %lld Java class name: Main
[PDF Link]
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 convertS into T in minimum number of moves. In each move, you can
- change a `0' in S to `1'
- change a `?' in S to `0' or `1'
- 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 (C200) 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
301??000010100110110001000000
Sample Output
Case 1: 3Case 2: 1Case 3: -1
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 5 using namespace std; 6 7 int main () 8 { 9 int n,len1,flag=1;10 char s[110],t[110];11 scanf("%d",&n);12 while (n--)13 {14 scanf("%s%s",s,t);15 len1=strlen(s);16 //len2=strlen(t);17 int a,b,c,aa,bb;18 a=b=c=aa=bb=0;19 for (int i=0; i<len1; i++)20 {21 if (s[i]=='0')22 a++;23 else if (s[i]=='1')24 b++;25 else26 c++;27 }28 for (int i=0; i<len1; i++)29 {30 if (t[i]=='0')31 aa++;32 else33 bb++;34 }35 if (b>bb)36 {37 printf ("Case %d: -1\n",flag++);38 continue;39 }40 int sum=0;41 int j;42 if (s[i]=='1'&&t[i]=='0')43 {44 for (j=0; j<len1; j++)45 {46 if (s[j]=='0'&&t[j]=='1')47 {48 sum++;49 s[i]='0';50 s[j]='1';51 break;52 }53 }54 if (j>=len1)55 break;56 57 }58 }59 for (int i=0; i<len1; i++)60 {61 int j;62 if (s[i]=='1'&&t[i]=='0')63 {64 for (j=0; j<len1; j++)65 {66 if (s[j]=='?'&&t[j]=='1')67 {68 sum++;69 s[i]='?';70 s[j]='1';71 break;72 }73 }74 if (j>=len1)75 break;76 }77 }78 for (int i=0; i<len1; i++)79 {80 if (s[i]=='0'&&t[i]=='1')81 sum++;82 //ans=sum+c;83 }84 printf ("Case %d: %d\n",flag++,sum+c);85 }86 return 0;87 }
- 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 机智题
- H题 hdu 2520 我是菜鸟,我怕谁
- I题 hdu 1234 开门人和关门人
- inner join, left join, right join, full join 的区别?
- Hadoop伪分布配置详解
- 算法之匈牙利算法
- UVA 12545 Bits Equalizer
- hdu 1281 棋盘游戏(二分匹配)
- hdu 1150 Machine Schedule(二分匹配,简单匈牙利算法)
- 随记
- hdu 5326 Work(杭电多校赛第三场)
- su 与su -
- hdu 5319 Painter(杭电多校赛第三场)
- hdu 5328 Problem Killer(杭电多校赛第四场)
- hdu 1151 Air Raid(最小路径覆盖)