[SPOILER] expert quiz #14
Daniel Wesenberg <carterwesenberg-/[email protected]> Wed, 12 Jul 2006 09:52:39 -0700 (PDT)
| Newsgroups | gmane.comp.lang.perl.qotw.discuss |
|---|---|
| Message-ID | <[email protected]> |
--0-1828353884-1152723159=:31963
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
Hi,
I read your quiz solution and can see how this program could apply to a problem I have. How might I modify this program so that I can get results for the longest repeated substring in 1.5 million "random" digits? And print right within the program.
Thanks in advance,
Dan
#!C:/Perl/bin/perl.exe -w
sub repeated_substring {
my $size = length $_[0];
my $maxsize = int ($size / 2);
my ($f_index, $f_offset) = (0,0);
OUTER:
for (my $index=0; $index < $size; $index++) {
# The big optimizer
# (added a bounds check to keep us from going beyond the
# end of the data)
if ($f_offset > 20 && $index + $f_offset <= $size) {
for (my $t_index = $index + $f_offset - 15;
$t_index >= $index;
$t_index = $t_index - 15) {
if (index($_[0], substr($_[0], $t_index,
$index+$f_offset-$t_index),
$index+$f_offset) == -1) {
$index = $t_index;
next OUTER;
}
}
}
for (my $offset=$f_offset+1;
$offset <= $maxsize && $index + $offset <= $size;
$offset++) {
if (index($_[0], substr($_[0], $index, $offset),
$index+$offset) >= 0) {
$f_index = $index;
$f_offset = $offset;
} else {
last;
}
}
}
return substr($_[0], $f_index, $f_offset);
}
---------------------------------
Do you Yahoo!?
Get on board. You're invited to try the new Yahoo! Mail Beta.
--0-1828353884-1152723159=:31963
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
<div>Hi,</div> <div> </div> <div>I read your quiz solution and can see how this program could apply to a problem I have. How might I modify this program so that I can get results for the longest repeated substring in 1.5 million "random" digits? And print right within the program.</div> <div>Thanks in advance,</div> <div> </div> <div>Dan</div> <div> </div> <div>#!C:/Perl/bin/perl.exe -w</div> <div>sub repeated_substring {<BR> my $size = length $_[0];<BR> my $maxsize = int ($size / 2);<BR> my ($f_index, $f_offset) = (0,0);</div> <div> OUTER:<BR> for (my $index=0; $index < $size; $index++) {</div>
<div> # The big
optimizer<BR> # (added a bounds check to keep us from going beyond the<BR> # end of the data)<BR> if ($f_offset > 20 && $index + $f_offset <= $size) {<BR> for (my $t_index = $index + $f_offset - 15;<BR> $t_index >= $index;<BR> $t_index = $t_index - 15) {<BR> if (index($_[0], substr($_[0],
$t_index,<BR> $index+$f_offset-$t_index),<BR> $index+$f_offset) == -1) {<BR> $index = $t_index;<BR> next OUTER;<BR> }<BR> }<BR> &nbs
p; }</div>
<div> for (my $offset=$f_offset+1;<BR> $offset <= $maxsize && $index + $offset <= $size;<BR> $offset++) {<BR> if (index($_[0], substr($_[0], $index, $offset),<BR> $index+$offset) >= 0) {<BR> $f_index = $index;<BR> $f_offse
t =
$offset;<BR> } else {<BR> last;<BR> }<BR> }<BR> }<BR> return substr($_[0], $f_index, $f_offset);<BR> }</div><p> 
<hr size=1>Do you Yahoo!?<br>
Get on board. <a href="http://us.rd.yahoo.com/evt=40791/*http://advision.webevents.yahoo.com/handraisers">You're invited</a> to try the new Yahoo! Mail Beta.
--0-1828353884-1152723159=:31963--