in reply to efficient method of matching a string against a list of substrings?

What you do is create a list of pointers that correspond to the location of each character in the input string. Then sort the pointers in alphabetic order. All you have to do now is sort your substrings in alphabetic order as well and step through each list once, matching as you go.

This might be more efficiently done in C++.

EDIT: How efficient do you need to be? Searching a 100,000-character string for 100,000 10-character substrings took me 57 seconds -

use strict; use warnings; my ($input, $str, @str, $t); $input .= chr(97 + int rand 26) for 1..100000; for (1..100000) { $str = ''; $str .= chr(97 + int rand 26) for 1..10; push @str, $str; } $t = time(); for (@str) { print "$_\n" if index($input, $str) != -1; } print time() - $t;
To get noticeably more efficient than this, you really need a lower level language like C++.
  • Comment on Re: efficient method of matching a string against a list of substrings?
  • Download Code