Codeforces GYM 100962E: Elvis Presley 题解

首先,如果A是B的前缀或者B是A的前缀,那么直接输出-1

否则一定有解

我们可以考虑到使得答案最小化,则应该尽量选取短的数

我们将A,B的所有的前缀记录一下,称这些前缀是危险的,那么我们进行一次宽搜,如果当前串不是危险的,就可以把它放入答案,并且不用这个串再扩展了,因为一旦这个串已经被放入答案,它所扩展出的串都是不合法的;如果当前串危险,就将串后面加0加1都放入队列中,用它继续进行扩展

我们可以确定的是,记A,B中长度较长的串的长度为len,则我们扩展出的所有串长度都不会超过len,因为超过len的串的长度小于len的前缀我们一定已经放入答案中了,所以我们的集合一定是一个有限集

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <utility>
#include <cctype>
#include <algorithm>
#include <bitset>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <cmath>
#include <ctime>
#define LL long long
#define LB long double
#define x first
#define y second
#define Pair pair<int,int>
#define pb push_back
#define pf push_front
#define mp make_pair
#define LOWBIT(x) x & (-x)
using namespace std;

const int MOD=1e9+7;
const LL LINF=2e16;
const int INF=2e9;
const int magic=348;
const double eps=1e-10;

inline int getint()
{
	char ch;int res;bool f;
	while (!isdigit(ch=getchar()) && ch!='-') {}
	if (ch=='-') f=false,res=0; else f=true,res=ch-'0';
	while (isdigit(ch=getchar())) res=res*10+ch-'0';
	return f?res:-res;
}

int A,B;
map<Pair,bool> danger;
int num1[48],num2[48],len1,len2;
vector<int> ans;
queue<Pair> q;

bool issuf(int x,int y)
{
	memset(num1,0,sizeof(num1));
	memset(num2,0,sizeof(num2));
	int i;
	for (i=0;i<=30;i++) if (x&(1<<i)) num1[i]=1,len1=i;
	for (i=0;i<=30;i++) if (y&(1<<i)) num2[i]=1,len2=i;
	reverse(num1,num1+len1+1);
	reverse(num2,num2+len2+1);
	for (i=0;i<=len1;i++) if (num1[i]!=num2[i]) return false;
	return true;
}

inline void Add(int cur,int len)
{
	int i;
	memset(num1,0,sizeof(num1));
	for (i=0;i<=len;i++) if (cur&(1<<i)) num1[i]=1;
	reverse(num1,num1+len+1);
	cur=0;
	for (i=0;i<=len;i++) if (num1[i]) cur|=(1<<i);
	ans.pb(cur);
}

int main ()
{
	int i;
	A=getint();B=getint();
	if (A>B) swap(A,B);
	if (issuf(A,B))
	{
		printf("-1\n");
		return 0;
	}
	ans.pb(A);ans.pb(B);
	danger.clear();
	int cur=0,len;
	for (i=0;i<=len1;i++) 
	{
		if (num1[i]) cur|=(1<<i);
		danger[mp(cur,i)]=true;
	}
	cur=0;
	for (i=0;i<=len2;i++)
	{
		if (num2[i]) cur|=(1<<i);
		danger[mp(cur,i)]=true;
	}
	A=0;for (i=0;i<=len1;i++) if (num1[i]) A|=(1<<i);
	B=0;for (i=0;i<=len2;i++) if (num2[i]) B|=(1<<i);
	q.push(mp(1,0));
	while (!q.empty())
	{
		cur=q.front().x;len=q.front().y;q.pop();
		if (!danger[mp(cur,len)])
		{
			Add(cur,len);
		}
		else
		{
			if (!(len==len1 && cur==A) && !(len==len2 && cur==B))
			{
				q.push(mp(cur,len+1));
				q.push(mp(cur|(1<<(len+1)),len+1));
			}
		}
	}
	sort(ans.begin(),ans.end());
	for (i=0;i<ans.size();i++) printf("%d ",ans[i]);
	printf("\n");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值