You mean matching only from the start?
Your sorting technique sounds clever, but the sorting itself isn't constant, I suppose something like O(n*log(n)).
IMHO the fastest solution should be a trie, if it's worth the overhead depends on the number of words to search.
Another simpler approach is abusing the regex engine, because from 5.10 on it has internal trie optimization and is f*ckingly fast!
$str = join "\0",@wordlist,
@reverts = map { "\0" . reverse $_ } @wordlist;
$pattern = '(' . join('|',@reverts) . ')' ;
@matches = ( $str =~ /$pattern/g );
thats untested pseudo-code I hope you get the idea! =)
UPDATE
now tested:
DB<131> @wordlist=qw(mamy dady bara foo bic ymamaa arabic );
=> ("mamy", "dady", "bara", "foo", "bic", "ymamaa", "arabic")
DB<132> $str = join "\0",@wordlist,
=> "mamy\0dady\0bara\0foo\0bic\0ymamaa\0arabic"
DB<133> $pattern = join '|',map { "\0" . reverse $_ } @wordlist;
=> "\0ymam|\0ydad|\0arab|\0oof|\0cib|\0aamamy|\0cibara"
DB<134> @matches = ( $str =~ /($pattern)/g );
=> ("\0ymam", "\0arab")
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.