#!/usr/bin/perl -w use strict; my %subst; my \$y = 0; while(my \$l = <>) { chomp \$l; my \$x = 0; for my \$d (split(//, \$l)) { \$subst{chr(97+\$y).chr(97+\$x)} = \$d; \$x++; } \$y++; } # print "ba=>", \$subst{'ba'}, "\n"; # print "Velky vysledek je ", f('kafemidej', 'kafe'), "\n"; # redukuj_k('emidej', 'e'); # print "Velky vysledek je ", f('kafcomidej', 'kafe'), "\n"; print "Velky vysledek je ", f('jahodacokoladalineckeandeliccimalina', 'kafe'); sub redukuj_k { my (\$input, \$char) = @_; my \$sum = 0; if (length(\$input) == 1) { if (\$input eq \$char) { print "Vracim 1\n"; return 1; } else { return 0; } } for my \$i (0 .. length(\$input)-2) { my \$in1 = \$input; substr(\$in1, \$i, 2) = \$subst{ substr(\$in1, \$i, 2) }; \$sum += redukuj_k(\$in1, \$char); print "Volam rekurzi redukuj_k(\$in1, \$char) (sum = \$sum)\n"; } return \$sum; } sub f { my (\$input, \$kafe) = @_; return 0 if !length(\$input); return 0 if !length(\$kafe); my \$sum = 0; my \$char = substr(\$kafe, 0, 1); if (length(\$kafe) == 1) { my \$pocet = redukuj_k(\$input, \$kafe); print "Zkousim redukuj_k(\$input, \$kafe) => \$pocet\n"; return \$pocet; } for my \$preflen (1 .. length(\$input)-1) { my \$prefix = substr(\$input, 0, \$preflen); print "Zkousim redukuj_k(\$prefix, \$char): "; my \$pocet = redukuj_k(\$prefix, \$char); print " => \$pocet\n"; if (\$pocet) { my \$kafe1 = substr(\$kafe, 1); my \$inp1 = substr(\$input, \$preflen); \$sum += \$pocet * f(\$inp1, \$kafe1); } } return \$sum; }