Gegeben: Zwei Zeichenketten s = s[1]s[2]...s[k] t = t[1]t[2]...t[l] Gesucht: Die Laenge der laengsten gemeinsamen Teilfolge von s und t. Sei LLGT ein (k+1)x(l+1) Array. Wir fuellen LLGT systematisch gemaess der LLGT-Rekursion auf. // Initialization for i := 0 to k LLGT[i,0] <- 0 for j := 0 to l LLGT[0,j] <- 0 // Implementing the recursion for i := 1 to k for j := 1 to l if s[i] = t[j] then LLGT[i,j] <- 1 + LLGT[i-1, j-1] else LLGT[i,j] <- max {LLGT[i-1, j], LLGT[i, j-1]} Das Ergebnis steht in LLGT[k,l]. Die Laufzeit ist O(kl). Nachdem LLGT berechnet ist, kann man auch die Teilfolge selber finden, indem man einen Weg durch LLGT rekonstruiert, der zu einer optimalen Loesung fuehrt (dh, wir rekonstruieren die Pfeile, die wir in der Vorlesung in der Tabelle vermerkt hatten). Dies geschieht von hinten. // a stack to store the result S <- new Stack // start at the end (i,j) <- (k,l) while (i > 0 and k > 0) if s[i] = t[j] then // if the current symbols are equal, we can include them into the // sequence (i,j) <- (i-1, j-1) S.push(s[i]) else // otherwise we need to determine which of the two choices led to the // maximum if LLGT[i, j-1] > LLGT[i-1, j] (i, j) <- (i, j-1) else (i, j) <- (i-1, j) // now S contains an optimal subsequence while not S.isEmpty() output S.pop()