
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Print all longest common sub-sequences in lexicographical order in C++
In this problem, we are given two string str1 and str2. Our task is to create a program to Print all longest common subsequences in lexicographical order.
Let’s take an example to understand the problem,
Input: str1 = “gfare” , str2 = “rfare”
Output: fare
Solution Approach
In this problem, we will find all possible longest common subsequences and store them in a 2D matrix using Dynamic programming. After this, we will print sorted output by searching characters from a to z in the LCS.
Program to illustrate the working of our solution,
Example
#include<iostream> #include<cstring> #define MAX 100 using namespace std; int LCSLength = 0; int DP[MAX][MAX]; int calcLCSLenght(string str1, string str2, int l1, int l2, int i, int j) { int &lcsLen = DP[i][j]; if (i==l1 || j==l2) return lcsLen = 0; if (lcsLen != -1) return lcsLen; lcsLen = 0; if (str1[i] == str2[j]) lcsLen = 1 + calcLCSLenght(str1, str2, l1, l2, i+1, j+1); else lcsLen = max(calcLCSLenght(str1, str2, l1, l2, i+1, j), calcLCSLenght(str1, str2, l1, l2, i, j+1)); return lcsLen; } void printAllLCS(string str1, string str2, int l1, int l2, char data[], int index1, int index2, int currentLCSlength) { if (currentLCSlength == LCSLength) { data[currentLCSlength] = '\0'; puts(data); return; } if (index1==l1 || index2==l2) return; for (char ch='a'; ch<='z'; ch++) { bool done = false; for (int i=index1; i<l1; i++) { if (ch==str1[i]) { for (int j=index2; j<l2; j++) { if (ch==str2[j] && calcLCSLenght(str1, str2, l1, l2, i, j) == LCSLength-currentLCSlength) { data[currentLCSlength] = ch; printAllLCS(str1, str2, l1, l2, data, i+1, j+1, currentLCSlength+1); done = true; break; } } } if (done) break; } } } int main() { string str1 = "xysxysx", str2 = "xsyxsyx"; int l1 = str1.length(), l2 = str2.length(); memset(DP, -1, sizeof(DP)); LCSLength = calcLCSLenght(str1, str2, l1, l2, 0, 0); char data[MAX]; cout<<"All longest common sub-sequences in lexicographical order are\n"; printAllLCS(str1, str2, l1, l2, data, 0, 0, 0); return 0; }
Output
All longest common sub-sequences in lexicographical order are xsxsx xsxyx xsysx xysyx xyxsx xyxyx
- Related Articles
- Print all the combinations of a string in lexicographical order in C++
- C++ Program to Find the Longest Subsequence Common to All Sequences in a Set of Sequences
- Print all distinct circular strings of length M in lexicographical order in C++
- Count all sub-sequences having product
- Find sum of sum of all sub-sequences in C++
- Print all sequences of given length in C++
- Last Substring in Lexicographical Order in C++
- Program to print the longest common substring using C++
- How to Sort Elements in Lexicographical Order (Dictionary Order) in Golang?
- Sort the words in lexicographical order in Java
- Sort the words in lexicographical order in C#
- Sort the words in lexicographical order in Python
- K-th Smallest in Lexicographical Order in C++
- C++ Program to Sort Elements in Lexicographical Order (Dictionary Order)
- Java Program to Sort Elements in Lexicographical Order (Dictionary Order)

Advertisements