0% found this document useful (0 votes)
4 views3 pages

Experiment No 10 Longest Common Subsequence

The document contains two C programs: one for finding the length of the Longest Common Subsequence (LCS) using recursion, and another for checking if four people can handshake without crossing. The LCS program outputs a length of 4 for the given strings 'AGGTAB' and 'GXTXAYB'. The handshake program determines that it is not possible for two of the four people to handshake without crossing, outputting 'No'.

Uploaded by

ssjcoe.deptaiml
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Experiment No 10 Longest Common Subsequence

The document contains two C programs: one for finding the length of the Longest Common Subsequence (LCS) using recursion, and another for checking if four people can handshake without crossing. The LCS program outputs a length of 4 for the given strings 'AGGTAB' and 'GXTXAYB'. The handshake program determines that it is not possible for two of the four people to handshake without crossing, outputting 'No'.

Uploaded by

ssjcoe.deptaiml
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Experiment No 10

Longest common subsequence

// C program to find longest common subsequence using

// recursion

#include <stdio.h>

#include <string.h>

// Function to return the maximum of two integers

int max(int a, int b)

// Return a if a is greater than b, otherwise return b

return (a > b) ? a : b;

// Function to find the length of the Longest Common

// Subsequence (LCS) using recursion

int lcsRecursive(char* X, char* Y, int m, int n)

// Base case: If either string is empty, LCS length is 0

if (m == 0 || n == 0)

return 0;

// If the characters match, include them in LCS and

// recur for the remaining strings

if (X[m - 1] == Y[n - 1])

return 1 + lcsRecursive(X, Y, m - 1, n - 1);

// If the characters do not match, recursively find LCS

// by excluding one character at a time

else

// Return the maximum of LCS by excluding either the

// last character of X or Y

return max(lcsRecursive(X, Y, m, n - 1),

lcsRecursive(X, Y, m - 1, n));

}
int main()

// First string

char X[] = "AGGTAB";

// Second string

char Y[] = "GXTXAYB";

// Length of first string

int m = strlen(X);

// Length of second string

int n = strlen(Y);

// Calculate and print the length of Longest Common

// Subsequence (LCS)

printf("Length of LCS is %d\n",

lcsRecursive(X, Y, m, n));

return 0;

Output:

Length of LCS is 4

15 Puzzle Problem

Check if 4 people can handshake with no cross

#include <stdio.h>

int main()

int a = 1, b = 8, c = 10, d = 2;

// It is possible that A is greater than B so for making

// correct range

int mini = (a < b) ? a : b;

int maxi = (a > b) ? a : b;


// Both lie between A and B means YES

if ((c > mini && c < maxi) && (d > mini && d < maxi)) {

printf("YES\n");

// Only one of them lies between A and B means NO

else if ((c > mini && c < maxi)

|| (d > mini && d < maxi)) {

printf("NO\n");

// None of them lies between A and B means YES

else {

printf("YES\n");

return 0;

Output
No

You might also like