longestPalindromicSubsequence.cpp
// this function will return both string and length using LCS:

this function will return both string and length using LCS.
this function will return both string and length using LCS .
void usingLongestCommonSubseuence(string p){
	string s1 = p ;
	reverse(p.begin(),p.end());
	string s2 = p;
	int n = p.length();
	int LCS[n+1][n+1]; 
	for(int i=0;i<=n;i++){
		for(int j=0;j<=n;j++){
			if(i==0 || j==0) LCS[i][j]=0;
			else if(s[i-1] == s2[j-1])
					LCS[i][j] = 1 + LCS[i-1][j-1];
			else LCS[i][j] = max(LCS[i-1][j], LCS[i][j-1]);
		}
	}
	cout<<LCS[n][n]<<endl;
	int ansSize = LCS[n][n];
	string lcs(ansSize, '\0');
	int row = n, col = n;
	while(row>0 && col > 0){
		if(s1[row-1] == s2[col-1]){
			lcs[ansSize-1] = s1[row-1];
			ansSize--,row--,col--;
		}
		else if(LCS[row-1][col] > LCS[row][col-1]){
			row--;
		}else col--;
	}
	cout<<lcs<<endl;
}
void usingLongestCommonSubseuence ( string p ) { string s1 = p ; reverse ( p . begin ( ) , p . end ( ) ) ; string s2 = p ; int n = p . length ( ) ; int LCS [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 || j == 0 ) LCS [ i ] [ j ] = 0 ; else if ( s [ i - 1 ] == s2 [ j - 1 ] ) LCS [ i ] [ j ] = 1 + LCS [ i - 1 ] [ j - 1 ] ; else LCS [ i ] [ j ] = max ( LCS [ i - 1 ] [ j ] , LCS [ i ] [ j - 1 ] ) ; } } cout << LCS [ n ] [ n ] << endl ; int ansSize = LCS [ n ] [ n ] ; string lcs ( ansSize , ' \0 ' ) ; int row = n , col = n ; while ( row > 0 && col > 0 ) { if ( s1 [ row - 1 ] == s2 [ col - 1 ] ) { lcs [ ansSize - 1 ] = s1 [ row - 1 ] ; ansSize -- , row -- , col -- ; } else if ( LCS [ row - 1 ] [ col ] > LCS [ row ] [ col - 1 ] ) { row -- ; } else col -- ; } cout << lcs << endl ; }
this function will return both string and length using LCS:
this function will return both string and length using LCS :
//LCS[i][j] is LCS of s1[0..i-1] and s2[0...j-1]
 //	for(int i=0;i<=n;i++){
 //		for(int j=0;j<=n;j++){
 //			cout<<LCS[i][j]<<" ";
 //		}
 //		cout<<endl;
 //	}
 //printing the LCS:
