Maximum 18 caractères par chaîne. Lettres uniquement recommandées.
3
étape 0 / 0
Phase 1 — Initialisation
Phase 2 — Remplissage
Phase 3 — Remontée
En cours
Match (+1)
Calculée
Remontée
Code C correspondant
char *plssc(char *s, char *t) {
int n = strlen(s), m = strlen(t);
int **l = creer_matrice(n+1, m+1);
/* Phase 1 — Initialisation */
for (int i = 0; i <= n; i++) { l[i][0] = 0; }
for (int j = 0; j <= m; j++) { l[0][j] = 0; }
/* Phase 2 — Remplissage */
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (s[i-1] == t[j-1]) {
l[i][j] = 1 + l[i-1][j-1]; /* match → diagonale */
} else {
l[i][j] = max(l[i-1][j], l[i][j-1]); /* max voisins */
}
}
}
/* Phase 3 — Remontée */
char *x = (char*)malloc(sizeof(char) * (l[n][m] + 1));
x[l[n][m]] = '\0';
int i = n, j = m, k = l[n][m] - 1;
while (k >= 0) {
if (l[i][j] == l[i-1][j]) { i--; }
else if (l[i][j] == l[i][j-1]) { j--; }
else { x[k] = s[i-1]; i--; j--; k--; }
}
detruire_matrice(l, n+1, m+1);
return x;
}