#include <stdio.h>
#include <string.h>
struct node { int x, y, flag; };

int DT[1010][1010], len1, len2;
char s1[1010], s2[1010], output[1010];
node back[1010][1010];

int max(int a, int b) { return (a>b?a:b); }

int main() {
    scanf("%s %s", s1+1, s2+1);
    len1=strlen(s1+1); len2=strlen(s2+1);
    
    for(int i=1; s1[i]; i++) {
        for(int j=1; s2[j]; j++) {
            if(s1[i]==s2[j]) {
                DT[i][j]=DT[i-1][j-1]+1;
                back[i][j]={i-1, j-1, 1};  
            }
            else {
                DT[i][j]=max(DT[i][j-1], DT[i-1][j]);
                if(DT[i][j]==DT[i][j-1]) back[i][j]={i, j-1, 0};
                else back[i][j]={i-1, j, 0};
            }
            //printf("%d|%d - (%d, %d : %d) ", i, j, back[i][j].x, back[i][j].y, back[i][j].flag);
        }
    }

    int x=len1, y=len2, ptr=DT[len1][len2];

    while(ptr) {
        if(back[x][y].flag==1) output[ptr--]=s1[x];
        int nx=back[x][y].x, ny=back[x][y].y;
        x=nx, y=ny;
    }
    printf("%d", DT[len1][len2]);
    if(DT[len1][len2]) printf("\n%s", output+1);
}

Incrustar en el sitio web

Para incrustar este proyecto en tu sitio web, copia el siguiente código y pégalo en el HTML de tu sitio web: