# Print all longest common sub-sequences in lexicographical order

You are given two strings.Now you have to print all longest common sub-sequences in lexicographical order?

Examples:

```Input : str1 = "abcabcaa", str2 = "acbacba"
Output: ababa
abaca
abcba
acaba
acaca
acbaa
acbca
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

This problem is an extension of longest common subsequence. We first find length of LCS and store all LCS in 2D table using Memoization (or Dynamic Programming). Then we search all characters from ‘a’ to ‘z’ (to output sorted order) in both strings. If a character is found in both strings and current positions of character lead to LCS, we recursively search all occurrences with current LCS length plus 1.

Below is the implementation of algorithm.

 `// C++ program to find all LCS of two strings in ` `// sorted order. ` `#include ` `#define MAX 100 ` `using` `namespace` `std; ` ` `  `// length of lcs ` `int` `lcslen = 0; ` ` `  `// dp matrix to store result of sub calls for lcs ` `int` `dp[MAX][MAX]; ` ` `  `// A memoization based function that returns LCS of ` `// str1[i..len1-1] and str2[j..len2-1] ` `int` `lcs(string str1, string str2, ``int` `len1, ``int` `len2, ` `                                      ``int` `i, ``int` `j) ` `{ ` `    ``int` `&ret = dp[i][j]; ` ` `  `    ``// base condition ` `    ``if` `(i==len1 || j==len2) ` `        ``return` `ret = 0; ` ` `  `    ``// if lcs has been computed ` `    ``if` `(ret != -1) ` `        ``return` `ret; ` ` `  `    ``ret = 0; ` ` `  `    ``// if characters are same return previous + 1 else ` `    ``// max of two sequences after removing i'th and j'th ` `    ``// char one by one ` `    ``if` `(str1[i] == str2[j]) ` `        ``ret = 1 + lcs(str1, str2, len1, len2, i+1, j+1); ` `    ``else` `        ``ret = max(lcs(str1, str2, len1, len2, i+1, j), ` `                  ``lcs(str1, str2, len1, len2, i, j+1)); ` `    ``return` `ret; ` `} ` ` `  `// Function to print all routes common sub-sequences of ` `// length lcslen ` `void` `printAll(string str1, string str2, ``int` `len1, ``int` `len2, ` `              ``char` `data[], ``int` `indx1, ``int` `indx2, ``int` `currlcs) ` `{ ` `    ``// if currlcs is equal to lcslen then print it ` `    ``if` `(currlcs == lcslen) ` `    ``{ ` `        ``data[currlcs] = ``'\0'``; ` `        ``puts``(data); ` `        ``return``; ` `    ``} ` ` `  `    ``// if we are done with all the characters of both string ` `    ``if` `(indx1==len1 || indx2==len2) ` `        ``return``; ` ` `  `    ``// here we have to print all sub-sequences lexicographically, ` `    ``// that's why we start from 'a'to'z' if this character is ` `    ``// present in both of them then append it in data[] and same ` `    ``// remaining part ` `    ``for` `(``char` `ch=``'a'``; ch<=``'z'``; ch++) ` `    ``{ ` `        ``// done is a flag to tell that we have printed all the ` `        ``// subsequences corresponding to current character ` `        ``bool` `done = ``false``; ` ` `  `        ``for` `(``int` `i=indx1; i

Output:

```ababa
abaca
abcba
acaba
acaca
acbaa
acbca
```

This article is contributed by Shashak Mishra ( Gullu ). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.