Create a C++ Header Function with DYNAMIC programing with the following details:
|
longest common subsequence |
|
input: a string a of length m and a string b of length n output: the longest string ssuch that s is a subsequence of both a and b; in the case of ties, use the substring that comes first alphabetically |
The dynamic programming algorithm for subsequences is similar to the one for substrings. Both involve a 2D array of strings, base cases, and a general case. However they are different problems so a few things are different. In the subsequence problem we use the invariant
A[i][j] = the longest common subsequence in a[:i] and b[:j].
This time, the subsequence in A[i][j] is not obligated to include the last character of a[:i] or b[:j]. The base cases are the same; when i=0 or j=0, the only possible subsequence is the empty string.
In the general case, when a[i-1]==a[j-1],we can include the last character a[i-1] in whatever subsequence was already computed for A[i-1][j-1]. But when a[i-1]a[j-1], we don’t have to settle for the empty string. Instead, we can form a subsequence that ignores the last characters of a[:i] and b[:j]. We have two to choose from, one at A[i-1][j] and another at A[i][j-1], and we prefer whichever is longer.
After the general cases have completed, A[m][n] contains a longest subsequence, but if it is tied with another subsequence, the other one might come first alphabetically. So there is a post-processing step of searching for the longest string, which can only be in the last row or last column.
The complete pseudocode is below.
dynamic_subsequence(a, b):
A = new (m+1)(n+1) 2D array of strings
Initialize each A[i][0]="" for all i [0, m]
Initialize each A[0][j]="" for all j [0, n]
for i from 1 to m inclusive:
for j from 1 to n inclusive:
if a[i-1] == b[j-1]:
A[i][j] = A[i-1][j-1] + a[i-1]
else if len(A[i-1][j]) > len(A[i][j-1]):
A[i][j] = A[i-1][j]
else:
A[i][j] = A[i][j-1]
Let s be the longest string in row A[m][?]and column A[?][n]; in the case of ties, prefer the string that comes first alphabetically
return s
Just like the substring algorithm, the bottleneck is the O(n) string creation operation, inside two nested loops, for a total of O(n3) time.
#include <iostream>
#include <vector>
using namespace std;
string dynamic_subsequence(string a, string b) {
int m = a.length();
int n = b.length();
string **dp = new string*[m+1];
for(int i=0; i<=m; i++) {
dp[i] = new string[n+1];
}
for(int i=0; i<=m; i++) {
dp[i][0] = "";
}
for(int i=0; i<=n; i++) {
dp[0][i] = "";
}
for(int i=1; i<=m; i++) {
for(int j=1; j<=n; j++) {
if(a[i-1] == b[j-1]) {
dp[i][j] = dp[i-1][j-1] + a[i-1];
} else if(dp[i-1][j].length() > dp[i][j-1].length()) {
dp[i][j] = dp[i-1][j];
} else {
dp[i][j] = dp[i][j-1];
}
}
}
int longestLen = dp[m][n].length();
string longestInRow = "";
string longestInCol = "";
for(int i=0; i<=m; i++) {
if(dp[i][n].length() > longestInCol.length()) {
longestInCol = dp[i][n];
}
}
for(int j=0; j<=n; j++) {
if(dp[m][j].length() > longestInRow.length()) {
longestInRow = dp[m][j];
}
}
string result = longestInCol;
if(result.length() == longestInRow.length() && longestInCol < longestInRow) {
result = longestInRow;
}
for(int i=0; i<=m; i++) {
delete [] dp[i];
}
delete [] dp;
return result;
}
int main() {
cout << dynamic_subsequence("ABBABAAAABBB", "PRAABBKKK") << endl;
}

Hi. Please find the answer above.. In case of any doubts, you may
ask in comments. You may upvote the answer if you feel i did a good
work!
Create a C++ Header Function with DYNAMIC programing with the following details: longest common subsequence input:...
this python code below is to find the longest common subsequence from two input string !!! however it give me the wrong answer, can any one fix it thanks def lcs(s1, s2): matrix = [["" for x in range(len(s2))] for x in range(len(s1))] for i in range(len(s1)): for j in range(len(s2)): if s1[i] == s2[j]: if i == 0 or j == 0: matrix[i][j] = s1[i] else: matrix[i][j] = matrix[i-1][j-1] + s1[i] else: matrix[i][j] = max(matrix[i-1][j], matrix[i][j-1], key=len) cs =...
2. (40 pts) Let A, B, and C be three strings each n characters long. We want to compute the longest subsequence that is common to all three strings. (a) Let us first consider the following greedy algorithm for this problem. Find the longest common subsequence between any pair of strings, namely, LCS(A, B) LCS(B, C), LCS(A, C). Then, find the longest common subsequence between this LCS and the 3rd string. That is, supposing that the longest common pair wise...
2. (40 pts) Let A, B, and C be three strings each n characters long. We want to compute the longest subsequence that is common to all three strings. (a) Let us first consider the following greedy algorithm for this problem. Find the longest common subsequence between any pair of strings, namely, LCS(A, B). LCS(B,C), LCS(A, C). Then, find the longest common subsequence between this LCS and the 3rd string. That is, supposing that the longest common pair wise subsequence...
Need help with all 3 parts.
Thanks
Question 1 (Longest Common Subsequence) In the longest common sub- sequence algorithm we discussed in class, we formulated the recursive formula based on prefixes of the two inputs, i.e., X[1...) and Y [1..,]. 1. Rewrite the recursive formula using suffixes instead of prefixes, i.e., X[...m] and Y[j..n]. 2. Develop a bottom-up dynamic programming algorithm based on the recur- sive formula in (a). Describe the algorithm and write a pseudo code. 3. Use the...
Question 2 (2.5 points) Longest common subsequence Consider the procedure to determine the length of the longest common subsequence, LCS- LENGTH(X, Y). It solves the LCS problem in a bottom-up manner, by filling out a 2-D tabular LCS-LENGTH(X, Y) m = X.length 2. n-Y.length 3. let cO.m, 0n] be a new array 4, for i = 0 to m for/ = 0 to n else if ATY ci,ci ,j-1 +1 10 else 12. return clm, n] For example, X (B,...
Questions 33 to 35 refer to the following Longest Common Subsequence problem. Given two sequences X-XI, X2,..., ...., X and Y y, y......... ya. Let C[ij]be the length of Longest Common Subsequence of x1, x2,..., Xi and y, y,..... Then Cij] can be recursively defined as following: CO if i=0 or j = 0 Cli,j][i-1.j-1]+1 ifi.j> 0 and x = y, max{C[i-1.7].[1.j-1); if i j>0 and x*y 0 The following is an incomplete table for sequence of AATGTT and AGCT....
The following table is partially filled. 0 1 4 0 Xi 4 D a) Explain why c[1,1] to c[1,5] and c[2,1] to c[5,1] are all 1s? b) Compute c[2,2], and which cell do you refer to when computing it? c) Compute c 2,31 and c[3,2], which cell do you refer to directly this time? d) Fill up the rest of the cells. Assume that you take c[i,j - 1] when there is a draw in line 11. (i.e., take the...
I really would appreciate some help with this assignment and
include comments please as I am in this for learning.
Part I: Maximum increasing subsequence (adapted from Programming Exercise 22.2) Write a program MaxlncreasingSubseq.java that prompts the user to enter a string and displays a maximum length increasing subsequence of characters. Here are four sample runs Enter a string: zebras ers Enter a string: Welcome Welo Enter a string: mmmoooooovwwvee mov Enter a string: abqrstcxyzdefghij abcdefghij You must use dynamic...
Please use Python
def findSubstrings(s):
# Write your code here
Consider a string, s = "abc". An alphabetically-ordered sequence of substrings of s would be {"a", "ab", "abc", "b", "bc", "c"}. If the sequence is reduced to only those substrings that start with a vowel and end with a consonant, the result is {"ab", "abc"}. The alphabetically first element in this reduced list is "ab", and the alphabetically last element is "abc". As a reminder: • Vowels: a, e, i,...
2. 9 marks] Strings. Consider the following definitions on strings Let U be the set of all strings Let s be a string. The length or size of a string, denoted Is, is the number of characters in s Let s be a string, and i e N such that 0 < ί < sl. We write s[i] to represent the character of s at index i, where indexing starts at 0 (so s 0] is the first character, and...