0

I am sorry if the title was misleading, but I really did not know how to address this.

Basically, the problem is the following: we have a bit vector T and a bit vector P.

Let's say P[a1], P[a2], ..., P[ak] are the 1 bits in P. I am interested in knowing, for each position i in T, how many bits of 1 are amongst P[i+a1], P[i+a2], ..., P[i+ak]. It's like putting P as a mask over the substring starting at i and checking how many bits of 1 there are in the end. If the number is impossible to get in good time, checking whether all the bits are 1 should suffice.

Is there a better algorithm that can solve this (better than the O(T*P) "naive" one of "sliding" the pattern from position to position and counting the number of occurences)?

Even a better constant of multiplication would be great in this case. I have heard you can use bitsets on C++ and get something like O( 1/32 * T * (T - P) ), but I am not very familiarized with bitsets and how they operate and such. Are there fast (&) operations on bitsets avaliable?

lbicsi
  • 63
  • 1
  • 9
  • Ah, the good old O(1/32 * N)... – Kerrek SB Jun 30 '15 at 08:17
  • So, you want to bitwise AND the bits from P and T together at various offsets? – samgak Jun 30 '15 at 08:32
  • When you say "for each position i in T", do you mean "for each position i in T *that has a 1-bit*"? Otherwise I can't see how T contributes – j_random_hacker Jun 30 '15 at 08:47
  • In any case: The operation you're performing is a kind of convolution. If the bit vectors are very long, you might be able to get a speedup by Fourier-transforming the two vectors, "multiplying" them in an appropriate way, and then transforming the result back. OTOH if 1-bits are sparse in T and the answer to my last question was "yes", then you can do it in time proportional to the number of 1-bits in T by "peeling off" the lowest-order 1-bit in T using `T & -T` and shifting P by multiplying by this amount. – j_random_hacker Jun 30 '15 at 08:51
  • Can you post the "naive" implementation to clarify what you actually need to do? The role of T is not clear and it seems I understood it differently from the others. – Serge Rogatch Jun 30 '15 at 08:58
  • It seems that some algorithm about search sub-string may be applied here. – Jarod42 Jun 30 '15 at 09:19

0 Answers0