Keep It Simple, Stupid PerlMonks

### Re: Analysing a (binary) string.

by ambidexter (Initiate)
 on Jun 28, 2013 at 05:40 UTC ( #1041138=note: print w/replies, xml ) Need Help??

in reply to Analysing a (binary) string. (Solved)

Hi, BrowserUk

If i understand it right, ! Since we dont know the length of the sub string that might repeat. It can be of length from 1-N/2. So if we start from 1 we need to parse the whole array N and for length 2 - (N-1) for length 3 - (N-2). So the time complexity of any algorithm for this problem would be "~N factorial". So if the string length is > 100 then it might take hours to get the complete solution !!

Replies are listed 'Best First'.
Re^2: Analysing a (binary) string.
by hdb (Monsignor) on Jun 28, 2013 at 12:23 UTC

I cannot see where you get N factorial from. To me it seems what you describe is N^2. This is also what I get from the brute force method below:

```use strict;
use warnings;
use Time::HiRes qw/time/;

sub find_best_offset {
my \$strref = shift;
my \$n = length \$\$strref;
my \$mindiff = \$n;
my \$best_offset = -1;
for( my \$i=1; \$i<=\$n/2; \$i++ ) {
# shift/rotate string and compare to original
my \$diff = \$\$strref ^ substr( \$\$strref, -\$i ).substr( \$\$strref
+, 0, -\$i );
# number of differing characters between shifted string and or
+iginal
my \$ndiff = \$diff =~ tr/\0//c;
# test for and keep optimal solution
if( \$ndiff < \$mindiff ) {
return \$i unless \$ndiff; # best case!
\$mindiff = \$ndiff;
\$best_offset = \$i;
}
}
return \$best_offset;
}

for ( 9, 99, 999, 9999, 99999 ) {
my \$t = time;
my \$str1 = "01234567890123456789".("01234567890123456x89" x \$_);
print find_best_offset( \\$str1 );
print "\t\$_\t",time-\$t,"\n";
}

Create A New User
Node Status?
node history
Node Type: note [id://1041138]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2018-03-18 02:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (228 votes). Check out past polls.

Notices?