Click here to Skip to main content
16,004,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the main function, how do i get user input array for char X and char Y? ie. instead of assigning value to char as Char X[ ]= "AGGTAB" or char Y[ ]= "GXTXAYB", how can i give it as an input?

eg:

INPUT:
AGGTAB
GXTXAYB

OUTPUT:
The length is 4

What I have tried:

C++
/* A Naive recursive implementation of LCS problem */
#include<bits/stdc++.h>

int max(int a, int b);


int lcs( char *X, char *Y, int m, int n )
{
if (m == 0 || n == 0)
	return 0;
if (X[m-1] == Y[n-1])
	return 1 + lcs(X, Y, m-1, n-1);
else
	return max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n));
}


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


int main()
{
    
  char X[] = "AGGTAB";
  char Y[] = "GXTXAYB";

int m = strlen(X);
int n = strlen(Y);

printf("Length of LCS is %dn", lcs( X, Y, m, n ) );

return 0;
}
Posted
Updated 23-Jan-18 11:25am
v3
Comments
OriginalGriff 23-Jan-18 12:03pm    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
Renshinzen 23-Jan-18 12:04pm    
Thanks...i'll work on it.
jeron1 23-Jan-18 12:03pm    

Hey man, it is C++ !

C++
#include <iostream>
#include <string>

int lcs( const std::string & X, const std::string  & Y, size_t m, size_t n )
{
  if (m == 0 || n == 0)
    return 0;
  if (X[m-1] == Y[n-1])
    return 1 + lcs(X, Y, m-1, n-1);
  else
    return std::max(lcs(X, Y, m, n-1), lcs(X, Y, m-1, n));
}

int main()
{
  std::string X,Y;

  getline(std::cin, X);
  getline(std::cin, Y);

  std::cout << "Length of LCS is " << lcs(X,Y, X.size(), Y.size()) << std::endl;
}
 
Share this answer
 
Use gets, _getws[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900