UVAlive 6152 Bits Equalizer 解题报告
2013-08-17 22:32
483 查看
题目
题意:
有两个串s和t,s 只由0,1,?组成,t只由0,1组成。每步操作可以对s:
1、将一个0转成1.
2、将一个?转成0或1.
3、交换两个字符的位置。
题解:
由于s的1的数量只能增多不能减少,所以若s本来的1的个数多于t,则无解。
否则统计对应位置不同的个数,分三种情况:s为?,s为1,s为0.后两种可以通过交换消掉一些,其余就直接加到答案即可。
//Time:9ms
题意:
有两个串s和t,s 只由0,1,?组成,t只由0,1组成。每步操作可以对s:
1、将一个0转成1.
2、将一个?转成0或1.
3、交换两个字符的位置。
题解:
由于s的1的数量只能增多不能减少,所以若s本来的1的个数多于t,则无解。
否则统计对应位置不同的个数,分三种情况:s为?,s为1,s为0.后两种可以通过交换消掉一些,其余就直接加到答案即可。
//Time:9ms
//Memory:0KB //Length:857B #include <iostream> #include <cstdio> #include <cstdlib> using namespace std; #define MAXN 1010 char s[MAXN],t[MAXN]; int main() { //freopen("/home/moor/Code/input","r",stdin); int ncase,n1,n2,n3,ans,one,two; scanf("%d",&ncase); for(int hh=1;hh<=ncase;++hh) { scanf("%s%s",s,t); n1=n2=n3=ans=0; one=0; two=0; for(int i=0;s[i];++i) { if(s[i]!=t[i]) { if(s[i]=='?') ++n3; else if(s[i]=='1') ++n1; else ++n2; } if(s[i]=='1') ++one; if(t[i]=='1') ++two; } printf("Case %d: ",hh); if(one>two) { printf("-1\n"); continue; } ans=n1+n2-min(n1,n2)+n3; printf("%d\n",ans); } return 0; }
相关文章推荐
- UVAlive 6160 Countdown 解题报告
- UVALive 5797解题报告
- 训练指南UVALive5088解题报告
- UVALive 6582 Magical GCD (数学+数据结构) -- 解题报告
- UVALive 6957 Hyacinth (DFS) -- 解题报告
- UVALive 5796 Hedge Mazes 解题报告
- UVALive 5790 Ball Stacking 解题报告
- UVALive 5791 Candy's Candy 解题报告
- UVALive 6195 The Dueling Philosophers Problem 解题报告
- UVALive 5791 Candy's Candy 解题报告
- Download Manager (HDU3233,UVA12231,UVALive4486) 解题报告
- UVALive 5986解题报告
- UVALive 5790 Ball Stacking 解题报告
- UVALive 6179 Windmill Animation 解题报告
- UVALive 5861 Hidden Terminal Problem 解题报告
- UVALive 5862 City Travel 解题报告
- UVAlive 3890&Poj3525 半平面交+二分 解题报告
- UVALive 5864 Register Allocation 解题报告
- UVAlive 5869 Airport 解题报告
- UVALive 6588 Crane (思维+贪心) -- 解题报告