I would use a lookup table, choosing the appropriate size (maybe 8 bits or 16 bits).
In this lookup table, I would associate each key with 4 values:
- 1 bit number attached to the left side
- number of 1 bits attached to the right side.
- the number of subsets in the middle that are not tied to anything
- the sizes of the subsets in the middle
For example, you can associate key 11011011
with 2,2,2 so that you know that the left adjacent byte with at least 1 bit attached to the right side will contain a subset whose size is + 2 (left attached length of the current byte) etc.
You need to find a way
- manage more than one subset in one key (e.g.
01011010
) - manage a key that has all 1s, so you have to consider the byte left and right and add the key length as part of the length of the subset.
but every key that has 0 on the first and last bits is trivially controlled, so you reduce the amount of processing needed for some possible keys.
I think it’s difficult to develop, but it can also be funny, and in the end you will just need to compare the keys, since everything else is hardcoded in the search table. Of course, I'm not sure that the last algorithm will surpass the simple approach, but, in my opinion, it is worth giving a chance.
source share