What is the best autocomplete/suggest algorithm,datastructure [C++/C]

前端 未结 5 1888
我在风中等你
我在风中等你 2020-12-07 07:56

We see Google, Firefox some AJAX pages show up a list of probable items while user types characters.

Can someone give a good algorithm, data structure for implementi

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-07 08:20

    For a simple solution : you generate a 'candidate' with a minimum edit (Levenshtein) distance (1 or 2) then you test the existence of the candidate with a hash container (set will suffice for a simple soltion, then use unordered_set from the tr1 or boost).

    Example: You wrote carr and you want car. arr is generated by 1 deletion. Is arr in your unordered_set ? No. crr is generated by 1 deletion. Is crr in your unordered_set ? No. car is generated by 1 deletion. Is car in your unordered_set ? Yes, you win.

    Of course there's insertion, deletion, transposition etc...

    You see that your algorithm for generating candidates is really where you’re wasting time, especially if you have a very little unordered_set.

提交回复
热议问题