H - XOR Matching 2 (XOR+思维)

 问题核心利用XOR运算的自反性,不过本题时间要求比较苛刻,能够优化的地方都要优化到最佳

详细的优化方法在代码注释中

#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <sstream>
#include <unordered_map>
#include <map>
#define int long long
#define endl '\n'
#define lowbit(x) x &(-x)
#define mh(x) memset(x, -1, sizeof h)
#define debug(x) cerr << #x << "=" << x << endl;
#define brk exit(0);
using namespace std;
const int N = 2e5;
const int M = 2 * N;
const int mod = 998244353;
typedef pair<int, int> PII;
typedef long long ll;
inline int read()
{
	register int x = 0, f = 1;
	register char c = getchar();
	while (c < '0' || c > '9')
	{
		if (c == '-')
			f = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9')
		x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
	return x * f;
}
inline void write(register int x)
{
	if (x > 9)
		write(x / 10);
	putchar(x % 10 + '0');
}
int a[N], b[N];
vector<int> ans;
map<int,int> st,vis;
int res[N];
void solve()
{
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	for (int i = 1; i <= n; i++)
	{
		cin >> b[i];
		st[b[i]]++;
	}
	for (int i = 1; i <= n; i++)//生成所有答案的可能性
	{
		for (int j = 1; j <= n; j++)
			ans.push_back(a[i] ^ b[j]);
	}
	sort(ans.begin(), ans.end());
	ans.erase(unique(ans.begin(), ans.end()), ans.end());//去重,降低复杂度,防止同一答案多次枚举
	int cnt = 0;
	for (int i = 0; i < ans.size(); i++)
	{
		vis.clear();
		int f = 1;
		for (int j = 1; j <= n; j++)
		{
			if(!st[ans[i]^a[j]])//这里要即时break
			{
				f = 0;
				break;
			}
			vis[a[j]^ans[i]]++;
		}
		if(f==0)
			continue;
		for (int j = 1; j <= n; j++)
		{
			if(vis[b[j]]!=st[b[j]])
			{
				f = 0;
				break;
			}
		}
		if (f)
			res[++cnt] = ans[i];

	}
	cout << cnt << endl;
	for (int i = 1; i <= cnt; i++)
		if (res[i] != 0)
			cout << res[i] << endl;
}
signed main()
{
	// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int T = 1;
	// cin >> T;
	while (T--)
		solve();
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值