Memoization ( 1D , 2D and 3D ) | A memoize recursive implementation of LCS problem ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] memoization applied in recursive solution ; base case ; if the same state has already been computed ; if equal , then we store the value of the function call ; store it in arr to avoid further repetitive work in future function calls ; store it in arr to avoid further repetitive work in future function calls ; Utility function to get max of 2 integers ; Driver Code
#include <bits/stdc++.h> NEW_LINE int arr [ 100 ] [ 100 ] [ 100 ] ; int max ( int a , int b ) ; int lcs ( char * X , char * Y , char * Z , int m , int n , int o ) { if ( m == 0 n == 0 o == 0 ) return 0 ; if ( arr [ m - 1 ] [ n - 1 ] [ o - 1 ] != -1 ) return arr [ m - 1 ] [ n - 1 ] [ o - 1 ] ; if ( X [ m - 1 ] == Y [ n - 1 ] and Y [ n - 1 ] == Z [ o - 1 ] ) { arr [ m - 1 ] [ n - 1 ] [ o - 1 ] = 1 + lcs ( X , Y , Z , m - 1 , n - 1 , o - 1 ) ; return arr [ m - 1 ] [ n - 1 ] [ o - 1 ] ; } else { arr [ m - 1 ] [ n - 1 ] [ o - 1 ] = max ( lcs ( X , Y , Z , m , n - 1 , o ) , max ( lcs ( X , Y , Z , m - 1 , n , o ) , lcs ( X , Y , Z , m , n , o - 1 ) ) ) ; return arr [ m - 1 ] [ n - 1 ] [ o - 1 ] ; } } int max ( int a , int b ) { return ( a > b ) ? a : b ; } int main ( ) { memset ( arr , -1 , sizeof ( arr ) ) ; char X [ ] = " geeks " ; char Y [ ] = " geeksfor " ; char Z [ ] = " geeksforgeeks " ; int m = strlen ( X ) ; int n = strlen ( Y ) ; int o = strlen ( Z ) ; printf ( " Length ▁ of ▁ LCS ▁ is ▁ % d " , lcs ( X , Y , Z , m , n , o ) ) ; return 0 ; }