Problem 496029 · hard · Phase 04 Non-Linear Data Structures

Loudest Checksum of a Short Burst

binary trie · prefix XOR · sliding window · counts and deletions

A sensor sends a stream of non-negative integer codes. A burst is a contiguous block of between 1 and limit consecutive codes, and its checksum is the XOR of all codes in it. Return the largest checksum over all bursts.

Examples

Input:  codes = [1, 2, 4, 8], limit = 2
Output: 12
Explanation: the bursts of length 1 or 2 are 1, 2, 4, 8, 1^2 = 3, 2^4 = 6, 4^8 = 12.

Input:  codes = [5, 1, 5, 6], limit = 4
Output: 7
Explanation: the whole stream gives 5^1^5^6 = 7; the best single code is only 6.

Input:  codes = [0, 0], limit = 1
Output: 0

Constraints

  • 1 <= len(codes) <= 3 * 10**4, 1 <= limit <= len(codes)
  • 0 <= codes[i] < 2**20
  • Target: about O(n * 20); trying every burst is O(n * limit) and too slow for long limits

Goals

  • Turn the XOR of a block into the XOR of two prefix values
  • Keep only the prefixes inside a sliding window in a binary trie with counts
  • Delete from a trie by decrementing counts and skip branches whose count is zero
Starting Python…