ProgrammingVery hardGATE CS · Competitive coding (CP)
What is the length of the longest common subsequence of ABCBDAB and BDCABA? The program computes it by plain recursion without a table. What is printed?
What is the length of the longest common subsequence of ABCBDAB and BDCABA? The program computes it by plain recursion without a table. What is printed?
#include <stdio.h>
const char *X = "ABCBDAB", *Y = "BDCABA";
int lcs(int i, int j) {
if (X[i] == '\0' || Y[j] == '\0') return 0;
if (X[i] == Y[j]) return 1 + lcs(i + 1, j + 1);
int a = lcs(i + 1, j), b = lcs(i, j + 1);
return a > b ? a : b;
}
int main(void) {
printf("%d", lcs(0, 0));
}- A. 3
- B. 4
- C. 5
- D. 6
Correct answer: B. 4
The recursion returns the longest common subsequence length of the two suffixes, and for these strings it is 4, for example BCBA.
More C Programming questions
- How many islands does this 4-connected flood-fill count find in the 3 by 3 grid "101", "010", "101"? Compute with the program. c #include…
- How many of these statements are true when x = 255? Count them with this program. c #include <stdint.h> #include <stdio.h> int main(void)…
- What is printed? c #include <stdio.h> int main(void) { unsigned a = 3; int b = -5; printf("%d", (a + b) > 0); }
- What is printed? c #include <stdio.h> #include <string.h> int main(void) { sizet n = 0; printf("%d", n - 1 > 100); }
- What is printed? c #include <stdio.h> int pal(const char s, int n) { int l = 0, r = n - 1; while (l < r) { if (s[l] != s[r]) return 0…
- What is printed? c #include <stdio.h> int main(void) { int m[3][5]; for (int i = 0; i < 3; i++) for (int j = 0; j < 5; j++) m[i][j] = i 5…