File: lcs

package info (click to toggle)
algotutor 0.8.6-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 640 kB
  • sloc: perl: 2,563; makefile: 41; php: 24; sh: 1
file content (142 lines) | stat: -rwxr-xr-x 3,901 bytes parent folder | download | duplicates (3)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/perl -w
# tk: longest common subsequence
# @: x¶Q http://www.cyut.edu.tw/~ckhung
# v: public domain (pGznNڪ{gΪj{,
#	jPijNzIH GPL)

# oӵ{JO algotutor @, ]iHbrҦU,
# ϥ Tk, W߰C
# W߰覡: ./lcs -e ansi 'AGCTATACGATGACT' 'GTCAGTATAGTCATATG'
# -e w "j" ܤ覡, ᭱i ansi  html

use Getopt::Std;
use strict;

my (%opts) = (
    e => "ansi",# type of emphasis, either "ansi" or "html"
);

my ($ES) = {
    ansi => { pre=>"\x1b[7m", post=>"\x1b[m", nl=>"\n" },
    html => { pre=>"<em>", post=>"</em>", nl=>"<br /><br />\n" },
};

use strict;

# longest common subsequence

sub lcs {
    my ($x, $y, $can) = @_;
    $x = [split //, " $x"];
    $y = [split //, " $y"];
    my ($show, $path, $len, $i, $j);
    if (ref $can) {
	$show = Board->new(-canvas=>$can, -width=>$#$x+1, -height=>$#$y+1);
	map { $show->cell(0,$_)->configure(-text=>$x->[$_], -status=>"done"); } 1..$#$x;
	map { $show->cell($_,0)->configure(-text=>$y->[$_], -status=>"done"); } 1..$#$y;
	$can->set_mark(1);
    }
    map { $len->[0][$_] = 0; } 0..$#$x;
    map { $len->[$_][0] = 0; } 0..$#$y;
    print " \\ ", join("   ", @{$x}[1..$#$x]) unless ref $can;
    my ($changed);
    for ($i=1; $i<=$#$y; ++$i) {
	print "\n$y->[$i]" unless ref $can;
	for ($j=1; $j<=$#$x; ++$j) {
	    if ($x->[$j] eq $y->[$i]) {
		$path->[$i][$j] = "\\";
		$len->[$i][$j] = $len->[$i-1][$j-1] + 1;
		if (ref $can) {
		    $show->cell(0,$j)->configure(-status=>"focus");
		    $show->cell($i,0)->configure(-status=>"focus");
		    $changed = $show->cell($i-1,$j-1);
		    $changed->configure(-status=>"focus");
		}
	    } elsif ($len->[$i-1][$j] >= $len->[$i][$j-1]) {
		$path->[$i][$j] = "^";
		$len->[$i][$j] = $len->[$i-1][$j];
		if (ref $can) {
		    $show->cell(0,$j)->configure(-status=>"discard");
		    $show->cell($i,0)->configure(-status=>"discard");
		    $changed = $show->cell($i-1,$j);
		    $changed->configure(-status=>"focus");
		}
	    } else {
		$path->[$i][$j] = "<";
		$len->[$i][$j] = $len->[$i][$j-1];
		if (ref $can) {
		    $show->cell(0,$j)->configure(-status=>"discard");
		    $show->cell($i,0)->configure(-status=>"discard");
		    $changed = $show->cell($i,$j-1);
		    $changed->configure(-status=>"focus");
		}
	    }
	    my ($s) = sprintf " $path->[$i][$j]%2d", $len->[$i][$j];
	    if (ref $can) {
		$can->set_mark(0);
		$show->cell(0,$j)->configure(-status=>"done");
		$show->cell($i,0)->configure(-status=>"done");
		$changed->configure(-status=>"done");
		$show->cell($i,$j)->configure(-text=>$s, -status=>"done");
		$can->set_mark(0);
	    } else {
		print $s;
	    }
	}
	$can->set_mark(1) if ref $can;
    }
    print "\n" unless ref $can;
    $i = $#$y; $j = $#$x;
    my ($x_hit, $y_hit, $prev);
    $prev = $show->cell(0,0) if ref $can;
    while ($i>0 and $j>0) {
	if (ref $can) {
	    $prev->configure(-status=>"done");
	    $prev = $show->cell($i,$j);
	    $prev->configure(-status=>"focus");
	}
	if ($path->[$i][$j] eq "\\") {
	    if (ref $can) {
		$show->cell($i,0)->configure(-status=>"focus");
		$show->cell(0,$j)->configure(-status=>"focus");
	    }
	    $x_hit->[$j--] = $y_hit->[$i--] = 1;
	} elsif ($path->[$i][$j] eq "^") {
	    --$i;
	} else {
	    --$j;
	}
	$can->set_mark(0) if ref $can;
    }
    if (ref $can) {
	$prev->configure(-status=>"done");
	$can->set_mark(0);
    }
    print mark_result($x, $x_hit);
    print mark_result($y, $y_hit);
}

sub mark_result {
    my ($str, $hit) = @_;
    my ($out, $i, $k);
    for ($i=1; $i<=$#$str; ++$i) {
	$out .= " ";
	$out .= $hit->[$i] ?
	    $ES->{$opts{e}}{pre} . $str->[$i] . $ES->{$opts{e}}{post} :
	    $str->[$i];
    }
    return $out . $ES->{$opts{e}}{nl};
}

if ($0 =~ /lcs$/) {

getopts('e:', \%opts);
my ($x, $y);
($x, $y) = $#ARGV >= 1 ? @ARGV[0,1] :
    ("AGCTATACGATGACT", "GTCAGTATAGTCATATG");
lcs($x, $y);

}

1;