baxy77bax has asked for the wisdom of the Perl Monks concerning the following question:
I have a little problem here. I also have a solution, but maybe someone has a better one. So the problem is as follows: I have a list of words like :
and i need to find out if there is a word which reverse can be matched to any other word in my set. Example:mamy dady bal foo bar ymamaa ...
The easiest solution here would be to create a reverse of every word, join them to the original set, label them , sort them (lex. of course). That way 'ymam' and 'ymamaa' would be one next to another and since I have labelled them I can say in O(c) (c=constant) time if there exists a match to the reverse of 'mamy' or not.(reverse) (match) mamy -> ymam -> ymamaa
Does anyone have a better solution to this ??
Thank you,
Baxy
Update: Thnx chordoba that is true, but i missed out one simple detail. These sets are huge. Therefore regex-ing would take much longer then my sorting suggestion. But yes you are correct (++) Thnx
Update : Yes only from the start ! :) That is also a good solution , but trie's are a bit memory heavy wouldn't you say. so that is why I thought sorting is the simplest one plus building a trie takes in practice much longer then sorting. Wouldn't you say?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: searching problem (reverse substrings)
by LanX (Saint) on Jan 18, 2013 at 10:33 UTC | |
Re: searching problem
by choroba (Cardinal) on Jan 18, 2013 at 10:27 UTC | |
by BillKSmith (Monsignor) on Jan 18, 2013 at 13:45 UTC | |
by Lotus1 (Vicar) on Jan 18, 2013 at 22:57 UTC | |
Re: searching problem
by RichardK (Parson) on Jan 18, 2013 at 11:58 UTC | |
Re: searching problem
by Khen1950fx (Canon) on Jan 18, 2013 at 17:24 UTC | |
by Lotus1 (Vicar) on Jan 18, 2013 at 22:27 UTC | |
Re: searching problem
by punch_card_don (Curate) on Jan 18, 2013 at 16:12 UTC |