in reply to Re: Re: Overlapping portions of sub strings
in thread Overlapping portions of sub strings
The AoA's is not a fundemental part of the algorithm, The version below is nearly identical except fro it reads the offset pairs from <DATA>, one pair per line instead of from an AoA.
The only memory constraint then becomes the length of the string $result, which is defined (though not automatically detected) by the largest offset.
As the process uses just one pass of two simple loops--one to read the offsets and build the result string, one to read the results string and extract the combined offsets--it should be considerably faster than any algorithm that use multiply recursive passes. You don't even need to sort the input offsets, and it outputs the combined offsets already sorted.
Hope it proves of some use.
Slightly modified code
#! perl -slw use strict; my $result = "\0" x 100; while (my ($start, $end) = split ' ', <DATA>) { substr($result, $start, $end - $start + 1) = 1 x ($end - $start + +1); } my (@results, $start); for (0 .. length $result) { my $c = substr($result, $_, 1); next if not $start and $c ne '1'; $start = $_ if $c eq '1' and not $start; next if $start and $c eq '1'; push @results, [$start, $_-1]; $start = undef; } print map{local $"='-'; "[@$_] "}@results; #!" __END__ 17 19 34 39 26 29 53 57 43 47 58 59 40 45 30 33 20 24 10 15 6 9 1 4 35 45 7 15
Examine what is said, not who speaks.
The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.
|
|---|