sub mkamatch { # (pattern,kmismatches)
use integer;
my $p = shift;
my $k = shift;
my $m = length($p);
$k = (10 * $m) / 100 + 1 unless defined $k;
my @p2 = map { 1 << $_ } 0 .. 31;
my @t = (0) x 256; # alphabet size
my $i = -1;
$t[$_] |= $p2[++$i] for (unpack("C*",$p));
my (@s,@r);
$#s = $#r = $k;
for ($r[0] = 0, my $i = 0; $i < $k; $i++){
$r[$i+1] = $r[$i] | $p2[$i];
}
sub {
my $key = shift;
my $n = length($key);
my $mb = $p2[$m-1];
my $ret = -1;
my @str = unpack("C*",$key);
for ($s[0] = 0, my $i = 0; $i < $n; $i++){
$s[0] = (($s[0] << 1) | 1) & (my $tc = $t[$str[$i]]);
for (my $j = 1; $j <= $k; $j++){
$s[$j] = (((($r[$j] << 1) & $tc) | (($r[$j-1] | $s[$j-
+1]) << 1)) | $r[$j-1]) | 1;
}
$ret = $i > $m ? $i - $m : 0, last if $s[$k] & $mb;
@r = @s;
}
$ret;
}
}
-
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.
|