implemented viterbi-like