题目
题意:
有两个串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; }