Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #define N 1001
- #define max(x, y) ((x > y) ? x : y)
- int L[N][N];
- int lcs_length(char A[N], char B[N]) {
- int i, j, m = strlen(A), n = strlen(B);
- for (i = m; i >= 0; i--) {
- for (j = n; j >= 0; j--) {
- if (A[i] == '\0' || B[j] == '\0') {
- L[i][j] = 0;
- } else if (A[i] == B[j]) {
- L[i][j] = 1 + L[i + 1][j + 1];
- } else {
- L[i][j] = max(L[i + 1][j], L[i][j + 1]);
- }
- }
- }
- return L[0][0];
- }
- int main () {
- int i = 0, j = 0, m, n, length;
- char a[N], b[N];
- scanf("%s", a);
- scanf("%s", b);
- m = strlen(a);
- n = strlen(b);
- length = lcs_length(a, b);
- if (length == 0) {
- printf("Empty");
- return 0;
- }
- while (i < m && j < n) {
- if (a[i] == b[j]) {
- printf("%c", a[i]);
- i++;
- j++;
- } else if (L[i+1][j] >= L[i][j+1]) {
- i++;
- } else {
- j++;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement