implement bm algorithm