【算法题解】选数异或

CSDN话题挑战赛第2期
参赛话题:学习笔记

a^b = x ==> abx = x^x ==> abx = 0 ==> abx^b = 0^b ==> a^x = ba^b = x ==> abx = x^x ==> abx = 0 ==> abx^b = 0^b ==> a^x = b

对于与a配对的数即为ax对于与a配对的数即为ax
Python带默认值的字典(Hash表)为defaultdictPython带默认值的字典(Hash表)为defaultdict

from collections import defaultdict

N = 100010
a = [0] * N
dp = [0] * N

n,m,x = (map(int,input().split()))
a[1:n+1] = list(map(int,input().split()))
last = defaultdict(int)

for i in range(1,n+1):
last[a[i]] = i
dp[i] = max(dp[i-1],last[x^a[i]])

for i in range(m):
l,r = (map(int,input().split()))
if dp[r] >= l:
print(“yes”)
else:
print(“no”)
初始化的时候设定默认值(defaultdict 或 dict.fromkeys)

from collections import defaultdict
aa = defaultdict(int)
aa[‘a’] = 1
aa[‘b’] = 2
aa
defaultdict(<class ‘int’>, {‘a’: 1, ‘b’: 2})
aa[‘c’]
0
aa
defaultdict(<class ‘int’>, {‘a’: 1, ‘b’: 2, ‘c’: 0})

aa = dict.fromkeys(‘abc’, 0)
aa
{‘a’: 0, ‘b’: 0, ‘c’: 0}

aa = defaultdict(lambda : 1)
aa[‘a’]
1

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值