-
Notifications
You must be signed in to change notification settings - Fork 33
/
longest_substring.pl
executable file
·51 lines (37 loc) · 1.12 KB
/
longest_substring.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/perl
# Finding the longest repeated substring
# Java code from:
# https://stackoverflow.com/questions/10355103/finding-the-longest-repeated-substring
use 5.010;
use strict;
use warnings;
no warnings 'recursion';
my $max_len = 0;
my $max_str = "";
sub insert_in_suffix_tree {
my ($root, $str, $index, $original_suffix, $level) = @_;
$level //= 0;
push @{$root->{indexes}}, $index;
if ($#{$root->{indexes}} > 0 && $max_len < $level) {
$max_len = $level;
$max_str = substr($original_suffix, 0, $level);
}
return if ($str eq q{});
my $child;
my $first_char = substr($str, 0, 1);
if (not exists $root->{children}{$first_char}) {
$child = {};
$root->{children}{$first_char} = $child;
}
else {
$child = $root->{children}{$first_char};
}
insert_in_suffix_tree($child, substr($str, 1), $index, $original_suffix, $level + 1);
}
my $str = @ARGV ? join('', <>) : "abracadabra";
my %root;
foreach my $i (0 .. length($str) - 1) {
my $s = substr($str, $i);
insert_in_suffix_tree(\%root, $s, $i, $s);
}
say "[$max_len]: $max_str";