open judge 特殊的密码锁 贪心

http://noi.openjudge.cn/ch0406/8469/

题面   ↑

这个题目有两次方法可以贪心,第一个开关动还是不动,枚举一下就可以了。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char a[1000],b[1000],c[1000];
int main()
{
	scanf("%s%s",a+1,b+1);
	int x=strlen(a+1),y=strlen(b+1);
	for(int i=1;i<=x;i++)
	c[i]=a[i];	
	int ans=0,ans2=0;
	ans2++;
	if(c[1]=='0') c[1]='1';
	else c[1]='0';
	if(c[2]=='0') c[2]='1';
	else  c[2]='0';
	for(int i=1;i<=x;i++)
	{
		if(c[i]!=b[i]&&i+1<=x)
		{	
			ans2++;
			c[i]=b[i];
			if(c[i+1]=='0') c[i+1]='1';
			else c[i+1]='0';
			if(c[i+2]=='0') c[i+2]='1';
	 		else c[i+2]='0';
		}
	}
	int h=0,h2=0;
	for(int i=1;i<=x;i++)
	{
		if(c[i]!=b[i])
		{
			h=1;
			break;
		}
	}
	for(int i=1;i<=x;i++)
	{
		if(a[i]!=b[i]&&i+1<=x)
		{
			ans++;
			a[i]=b[i];
			if(a[i+1]=='0') a[i+1]='1';
			else a[i+1]='0';
			if(a[i+2]=='0') a[i+2]='1';
			else a[i+2]='0';
		}
	}
	for(int i=1;i<=x;i++)
	{
		if(a[i]!=b[i])
		{
			h2=1;
			break;
		}
	}
	if(h2==0&&h==0)
	{
		int tq=min(ans,ans2);
		cout<<tq;
	}
	else if(h2==0&&h==1)
		cout<<ans;
	else if(h==0&&h2==1)
		cout<<ans2;
	else  puts("impossible");
	return 0;
}

 

转载于:https://my.oschina.net/u/3009052/blog/786102

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值