
- 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
Program to find length of longest common subsequence in C++
Suppose we have two strings text1 and text2, we have to find the length of their longest common subsequence. As we know a subsequence of a string is a new string generated from the original string with some characters deleted without changing the relative order of the remaining characters. (So for example "abe" is a subsequence of "abcde" but "adc" is not). A common subsequence of two strings is a subsequence that is common to both strings. So If there is no common subsequence, return 0. If the input is like “abcde”, and “ace”, then the result will be 3.
To solve this, we will follow these steps −
n := size of s, m := size of x
if either n is 0, or m is 0, then return 0
s := empty string, concatenated with s
x := empty string, concatenated with x
ret := 0
define a matrix dp of order (n + 1) x (m + 1)
for i in range 1 to n
for j in range 1 to m
dp[i, j] := max of dp[i, j - 1] and dp[i – 1, j]
if s[i] = x[j], then
dp[i, j] := max of dp[i, j], 1 + dp[i – 1, j – 1]
return dp[n, m]
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: int longestCommonSubsequence(string s, string x) { int n = s.size(); int m = x.size(); if(!n || !m) return 0; s = " " + s; x = " " + x; int ret = 0; vector < vector <int> > dp(n + 1, vector <int>(m + 1)); for(int i = 1; i <= n; i++){ for(int j = 1; j <= m ; j++){ dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]); if(s[i] == x[j]) { dp[i][j] = max(dp[i][j], 1 + dp[i - 1][j - 1]); } } } return dp[n][m]; } }; main(){ Solution ob; cout << (ob.longestCommonSubsequence("abcde", "ace")); }
Input
"abcde" "ace"
Output
3
- Related Articles
- Program to find length of longest common subsequence of three strings in Python
- Program to find length of longest balanced subsequence in Python
- Program to find length of longest anagram subsequence in Python
- Program to find length of longest bitonic subsequence in C++
- Program to find length of longest increasing subsequence in Python
- Program to find length of longest palindromic subsequence in Python
- Program to find length of longest fibonacci subsequence in Python
- Program to find length of longest circular increasing subsequence in python
- C++ Program for Longest Common Subsequence
- Java Program for Longest Common Subsequence
- Longest Common Subsequence
- Program to find length of longest common substring in C++
- Program to find out the length of longest palindromic subsequence using Python
- Program to find length of longest arithmetic subsequence with constant difference in Python
- Longest Common Subsequence in C++
