void PrintLCS(int b[][MAXLEN], char *x, int i, int j) { if(i == 0 || j == 0) return; if(b[i][j] == 0) { PrintLCS(b, x, i-1, j-1); printf("%c ", x[i-1]); } else if(b[i][j] == 1) PrintLCS(b, x, i-1, j); else PrintLCS(b, x, i, j-1); }
void PrintLCS(char x[], char y[], char b[][strlen(y)], int i, int j) { if (i==0 || j == 0) { return; } if (b[i][j] == '\\') { PrintLCS(x, y, b, i-1, j-1); printf("%c", x[i]); } else if (b[i][j] == '|') { PrintLCS(x, y, b, i-1, j); } else { PrintLCS(x, y, b, i, j-1); } }
/*recursive function for printing the LCS*/ void PrintLCS(int i,int j) { if(i==0 || j==0) return; if(b[i][j]==100) { PrintLCS(i-1,j-1); printf("%c ",x[i]); } else if(b[i][j]==101) PrintLCS(i-1,j); else PrintLCS(i,j-1); }
int main(int argc, char **argv) { char x[MAXLEN] = {"ABCBDAB"}; char y[MAXLEN] = {"BDCABA"}; int b[MAXLEN][MAXLEN]; int c[MAXLEN][MAXLEN]; int m, n; m = strlen(x); n = strlen(y); LCSLength(x, y, m, n, c, b); PrintLCS(b, x, m, n); PrintArray(); return 0; }
int main() { char x[] = {'0', 'a', 'b', 'd','b','\0'}; char y[] = {'0', 'b', 'd', 'c', 'b','\0'}; //x[strlen(x)] = '\0'; //y[strlen(y)] = '\0'; char b[strlen(x)][strlen(y)]; LCS(x, y, b); int i, j; int m = strlen(x); int n = strlen(y); for(i = 0; i < m; i++) { for(j = 0; j < n; j++) { printf("%c", b[i][j]); } printf("\n"); } printf("\n"); printf("Najdluzszy wspolny podciag dla %s i %s to: \n", x, y); PrintLCS(x, y, b, strlen(x)-1, strlen(y)-1); getchar(); return 0; }
void main() { /*Initializing the 0th place of x[] & y[] char array*/ x[0]='a';y[0]='z'; /*scanning the first sequence in x[] (from 1st place)*/ printf("give the first subsequence:\n"); gets(&x[1]); /*scanning the second sequence in y[] (from 1st place)*/ printf("give the second subsequnce:\n"); gets(&y[1]); LCS_Length(); printf("The length of the longest subsequence is %d\n",c[m][n]); printf("Here is the longest subsequences:\n"); PrintLCS(m,n); printf("\n"); }