aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-065/cheok-yin-fung/perl/ch-2.pl45
1 files changed, 32 insertions, 13 deletions
diff --git a/challenge-065/cheok-yin-fung/perl/ch-2.pl b/challenge-065/cheok-yin-fung/perl/ch-2.pl
index ab3d24fd87..b2bfbe0704 100644
--- a/challenge-065/cheok-yin-fung/perl/ch-2.pl
+++ b/challenge-065/cheok-yin-fung/perl/ch-2.pl
@@ -54,25 +54,43 @@ sub part_func { #partitions, generated by binary strings
}
my %hresult;
+my %sizeresult;
my $n = length $S;
for my $seperator (2**($n-1)+1..2**$n) {
my @p = grep {is_pali $_} part_func($S, $seperator);
- my $r = join ",", @p unless @p == ();
+
+ #from Perl Cookbook Extracting Unique Elements
+ my %seen = ();
+ my @p_uniq;
+ foreach my $item (@p) {
+ push @p_uniq, $item unless $seen{$item}++;
+ }
+ my $r = join ",", @p_uniq unless @p_uniq == ();
$hresult{$r} = 1;
+ $sizeresult{$r} = $#p_uniq + 1;
}
sub need_to_remove_subsequence {
- if (
- index($_[0], ",".$_[1]) == -1
- and
- index($_[0], $_[1].",") == -1
- ) {
- return 0;
+ my @in = @_;
+ my $bigbrother = shift @in;
+ my $number_of_components = 0;
+ for my $kid (@in) {
+ if (index(",".$bigbrother."," , ",".$kid.",") != -1) {
+ $number_of_components++;
+ }
+ }
+ if ( #all components of $pierre is inside $peter
+ $number_of_components == $#in+1
+ and
+ #avoid $peter permutes -> $pierre
+ $sizeresult{$bigbrother} > $#in+1 )
+ {
+ return 1;
}
else {
- return 1;
+ return 0;
}
}
@@ -81,17 +99,18 @@ sub need_to_remove_subsequence {
my @aresult = keys %hresult;
for my $peter (@aresult) {
for my $pierre (@aresult) {
- unless ($peter eq $pierre or $peter eq $S) {
- if (need_to_remove_subsequence($peter,$pierre)) {
+ unless ( $peter eq $pierre or $peter eq $S) {
+ if ( need_to_remove_subsequence( $peter, (split /,/, $pierre) )) {
delete $hresult{$pierre};
}
}
}
}
+
#print answer
-print "string: ", $S,"\n\n";
+print "string: ", $S,"\n";
print join "\n", sort keys %hresult;
print "\n";
@@ -99,12 +118,12 @@ print "\n";
# abaaba ->
# aa
# baab
-# aba, aba
+# aba
# abaaba
#
# aabaab -> #Example 1
# aabaa
-## aa, baab
+# aa, baab
# aba
#
# abbaba -> #Example 2