<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#use diagnostics
print "Input an RNA string using capital letters A,U,C,G, and no spaces between letters in the string\n";
$string = &lt;&gt;;
chomp $string;
@string = split (//,$string);
$stringlength = @string;
print "@string\n";
#print "$stringlength\n";
print "The number of matchings in each interval (remember that the empty matching is a matching)\n";

foreach $i (1..$stringlength) {
	 $S[$i][$i] = 1;
	 $S[$i][$i-1] = 1;
 }
# print "j, i, k\n";

 foreach $j (1 .. ($stringlength -1)) {
	 for ($i = $j-1; $i &gt;= 0; $i--) {
             $S[$i][$j] = $S[$i+1][$j-1];
            # print "$S[$i][$j]\n";

		 for ($k = $j; $k &gt; $i ; $k--) {
			# print "$j, $i, $k \n";
                   if (complementary($string[$k], $string[$i])) {
#                     print "$string[$k], $string[$i]\n";
                      $S[$i][$j] += $S[$i+1][$k-1] * $S[$k+1][$j];
#             print "$S[$i][$j]\n";
                   }
                 }
		 for ($k = $j-1; $k &gt; $i ; $k--) {
			# print "$j, $i, $k \n";
                   if (complementary($string[$k], $string[$j])) {
                      $S[$i][$j] += $S[$i+1][$k-1] * $S[$k+1][$j-1];
#             print "$S[$i][$j]\n";
                   }

		 }
print "The number of matchings in interval $i, $j is $S[$i][$j]\n";
	 }

 }
print "The number of matchings is $S[0][$stringlength-1]\n";

sub complementary {
  $first = shift (@_);
  $second = shift (@_);
    if (($first eq 'A') and ($second eq 'U')) {
     return 1;
    }
    if (($first eq 'U') and ($second eq 'A')) {
     return 1;
    }
    if (($first eq 'C') and ($second eq 'G')) {
     return 1;
    }
    if (($first eq 'G') and ($second eq 'C')) {
     return 1;
    }
return 0;
}
</pre></body></html>