题解 | #子数组异或和为0的最多划分#

子数组异或和为0的最多划分

http://www.nowcoder.com/practice/77e9828bbe3c4d4a9e0d49cc7537bb6d

n = int(input())
nums = list(map(int, input().split()))
dp = [0] * n
before = {0: -1}
before[nums[0]] = 0
dp[0] = 0 if nums[0] != 0 else 1
eor = nums[0]
for i in range(1, n):
    eor ^= nums[i]
    if eor in before:
        index = before[eor]
        if index == -1:
            dp[i] = 1
        else:
            dp[i] = dp[index] + 1
    before[eor] = i
    dp[i] = max(dp[i - 1], dp[i])
print(dp[-1])

全部评论

相关推荐

点赞 评论 收藏
分享
M_bao:换个排版吧哥们,看着费劲
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务