Question

In: Computer Science

Write a program that reads two strings from an input file (The first line is X,...

Write a program that reads two strings from an input file (The first line is X, the second line is Y), compute the longest common subsequence length AND the resulting string.

You will need to write 2 methods

1) return LCS length in iterative function

// return the length of LCS. L is the 2D matrix, X, Y are the input strings, m=|X|, n=|Y|

int lcs_it(int **C, string X, string Y, int m, int n )

2) return LCS resulting string in recursive function

// return the resulting LCS string.

string backtrack(int **C, string X, string Y, int m, int n)

Hint:

Compute the length of LCS requires to use 2D matrix where the row is |X|+1, column is |Y|+1. And the 2D matrix result should be used to construct the resulting string. Below is how to create a 2D array and how to pass it to the lcs_it function.

The methods will be tested in the main method as the following.

===== main ====================

int m=X.size();

int n=Y.size();

int **C=new int*[m+1];
for (int i=0; i<m+1;++i) C[i]=new int [n+1];

int len=lcs_it(C, X, Y, m, n);

cout<<" LCS is "<<backtrack(C, X, Y, m, n)<<endl;

'

here is input.txt :

ACCGTCTTAGCGATCAACACATTTAACAACGCGCCGCACCCCCCGTCAAACGAGCTTTTGGGCTCTTGTCCTTTTACAAGCTTCACGACGCATACAGCCTTGATCAACGGTTTGATCTGTCTCCCTTCAGCTGGCTTTAAAGGACATACATATGAAGGCCTTAATAAGGTCCGGGAACTCCACATATTCGGTACTGGGCAAACCCCATGAACCACCTCAACATGAAGAGTCCGAGGACTCTCACGATCCACCAATGCAGATCGGAACTGTGCGATCGCGTAATGAGCCGAGTACTTGGTTTGTGTTTAGGTTATGGGGGCCGGGAGCCGGTTCAATATAAGGAAGTAGTTGCAGATTAGTTGTTGCGAACGGTCATAAATTTGATGGGTAAACGTGAACTTAACAAACCGTGATAGCTAATCCTATGCATCCCTTACGTGGATCGACTCGAGTACCCAGGTGAACCGACTACTTGATAACCGGAAATCGCGGTATAAAAGCGCTCACGGTCAGGAGATATACCTCCAAGCAGTAGTCTTTCTGAGCCTAGAGTAGTAAATTACAGGGACGATGTCTTTTACCGAGGCAACATTTTATTGAGAATCACATGAGGCACAGGTAAAGGCGACATCACGATCGAGATCAACCCCTACTTGTTCAAAACATTGAGAACCAGCTCTGTTTTGGAACCTAGAAAGATAACGCATCCGCTTGATATTCCACGGCTTGTCCCTCTTGTGCGGTCCATCTATCGGAGTTTCCTCCGATACGACCCGCAATGTTTCCAGGCGTACGGTACTTTATGAATACACTCGCGCTGTAACCTGTTATGTGAAACACACACGACAGAGCTTCGCGTGGGCCCAGCGACCCGGTAATACTACATCACCGCACACGACCTCGAGCAGTCTTTGCCGGCGTCCGTAAGTAGTCTAAAGTTGTGTTGATGCTTGGGGTTAAAGCTAAATCGTCCGCAGAATACGACTCTCATCCCAAT
ACCCGCACGCGCTTTGGTCTAGATTCTAGCTCCAACTTGCCTGCTAGATACTCTGTTAAAAGATGGTTTTACAACCCCCTCCTCTGTCCCTGGGGTATTATATAATACGTCGGATAGTCAGGTACAAATACAAGTGGGTGGGAATACTTTTCCTCGGATCCTAGACCACGGATTACTGCGTGGTTGACAAGAGTCGGCCCGGAGGGAAACGTGAAGGTTAGTGCAATTAAAGTCTCTAATGTGAAGCCTCCGCGAAGCGAGGAGTTTCTGAGATCGAGTACTATTTAGAGTTCGAAATCACGGCTTAACCTCACTGCCACGCATAACTTGCCGGCAATCCAGTTTTGCAACGATACTTAATTTGTGCAGCTCATCTTTGCTGTCCAGAAATAGAGCTAGTCGATCTCATCTTGCGGGTAGCCAGAAGTCCTACCGTCTCCTCCATGTAGCTTAAAAATTTCGGTGAGGATCAAAAATGATAAACGTGACAGGTAAGCTCCTACGTCTATCCTATGACCCCCGCGGCAGAATAGGTTGGTAGTGTTAGTGCGTGAGCTGGTAGAATAGAGCACACTTAGGGAAACGGGAACCGTTATGTAGGGCTGCGACACACAAAAAAGTGTTCGTTGGTAAGCTGCCTCTCCACTAAACAGGATTTCTCTGGATGATCCCATCGAAGCAAGTTACGCACCACGCCGAGGCGGACCCTGGTACTAGCTGCCCCCCCCTTTATGGGGCGCTCGTACATCAAGATGATCGCGGACTCAACCTGATTACGAGTTGTCCAAGTAGTCCAGGGTAAGAGAAACTGGAGAGA

Solutions

Expert Solution

The following main program in C++ does the required tasks:

#include <iostream>
#include <fstream>

using namespace std;

//LCS iterative function
int lcs_it(int **C, string X, string Y, int m, int n) {
    //Initialize first row and column to be 0
    for(int i=0;i<=n;i++) C[0][i] = 0;
    for(int i=0;i<=m;i++) C[i][0] = 0;
    
    //Iteratively construct solution
    for(int i=1;i<=m;i++) {
        for(int j=1;j<=n;j++) {
            if(X.at(i-1) == Y.at(j-1)) C[i][j] = C[i-1][j-1]+1;
            else if(C[i-1][j]>C[i][j-1]) C[i][j] = C[i-1][j];
            else C[i][j] = C[i][j-1];
        }
    }
    return C[m][n];
}

string backtrack(int **C, string X, string Y, int m, int n) {
    //C now contains the required length values
    //Just construct the LCS from it
    //Start from end
    string lcs = "";
    int i=m,j=n;
    while(i>0 && j>0) {
        if(X.at(i-1) == Y.at(j-1)) {lcs.insert(0,1,X.at(i-1));i--;j--;}
        else if(C[i][j] == C[i-1][j]) {i--;}
        else j--;
    }
    return lcs;
}
int main()
{
    ifstream f1;
    f1.open("input.txt");
    string X;
    string Y;
    getline(f1,X);
    getline(f1,Y);
    int m = X.size();
    int n = Y.size();
    int **C=new int*[m+1];
    for (int i=0; i<m+1;++i) C[i]=new int [n+1];

    int len = lcs_it(C, X, Y, m, n);
    cout <<len << endl;
    
    cout<<" LCS is "<< backtrack(C, X, Y, m, n) <<endl;
    return 0;
}

Create a file input.txt in the same folder as the above cpp program with the contents given iin the question:

ACCGTCTTAGCGATCAACACATTTAACAACGCGCCGCACCCCCCGTCAAACGAGCTTTTGGGCTCTTGTCCTTTTACAAGCTTCACGACGCATACAGCCTTGATCAACGGTTTGATCTGTCTCCCTTCAGCTGGCTTTAAAGGACATACATATGAAGGCCTTAATAAGGTCCGGGAACTCCACATATTCGGTACTGGGCAAACCCCATGAACCACCTCAACATGAAGAGTCCGAGGACTCTCACGATCCACCAATGCAGATCGGAACTGTGCGATCGCGTAATGAGCCGAGTACTTGGTTTGTGTTTAGGTTATGGGGGCCGGGAGCCGGTTCAATATAAGGAAGTAGTTGCAGATTAGTTGTTGCGAACGGTCATAAATTTGATGGGTAAACGTGAACTTAACAAACCGTGATAGCTAATCCTATGCATCCCTTACGTGGATCGACTCGAGTACCCAGGTGAACCGACTACTTGATAACCGGAAATCGCGGTATAAAAGCGCTCACGGTCAGGAGATATACCTCCAAGCAGTAGTCTTTCTGAGCCTAGAGTAGTAAATTACAGGGACGATGTCTTTTACCGAGGCAACATTTTATTGAGAATCACATGAGGCACAGGTAAAGGCGACATCACGATCGAGATCAACCCCTACTTGTTCAAAACATTGAGAACCAGCTCTGTTTTGGAACCTAGAAAGATAACGCATCCGCTTGATATTCCACGGCTTGTCCCTCTTGTGCGGTCCATCTATCGGAGTTTCCTCCGATACGACCCGCAATGTTTCCAGGCGTACGGTACTTTATGAATACACTCGCGCTGTAACCTGTTATGTGAAACACACACGACAGAGCTTCGCGTGGGCCCAGCGACCCGGTAATACTACATCACCGCACACGACCTCGAGCAGTCTTTGCCGGCGTCCGTAAGTAGTCTAAAGTTGTGTTGATGCTTGGGGTTAAAGCTAAATCGTCCGCAGAATACGACTCTCATCCCAAT
ACCCGCACGCGCTTTGGTCTAGATTCTAGCTCCAACTTGCCTGCTAGATACTCTGTTAAAAGATGGTTTTACAACCCCCTCCTCTGTCCCTGGGGTATTATATAATACGTCGGATAGTCAGGTACAAATACAAGTGGGTGGGAATACTTTTCCTCGGATCCTAGACCACGGATTACTGCGTGGTTGACAAGAGTCGGCCCGGAGGGAAACGTGAAGGTTAGTGCAATTAAAGTCTCTAATGTGAAGCCTCCGCGAAGCGAGGAGTTTCTGAGATCGAGTACTATTTAGAGTTCGAAATCACGGCTTAACCTCACTGCCACGCATAACTTGCCGGCAATCCAGTTTTGCAACGATACTTAATTTGTGCAGCTCATCTTTGCTGTCCAGAAATAGAGCTAGTCGATCTCATCTTGCGGGTAGCCAGAAGTCCTACCGTCTCCTCCATGTAGCTTAAAAATTTCGGTGAGGATCAAAAATGATAAACGTGACAGGTAAGCTCCTACGTCTATCCTATGACCCCCGCGGCAGAATAGGTTGGTAGTGTTAGTGCGTGAGCTGGTAGAATAGAGCACACTTAGGGAAACGGGAACCGTTATGTAGGGCTGCGACACACAAAAAAGTGTTCGTTGGTAAGCTGCCTCTCCACTAAACAGGATTTCTCTGGATGATCCCATCGAAGCAAGTTACGCACCACGCCGAGGCGGACCCTGGTACTAGCTGCCCCCCCCTTTATGGGGCGCTCGTACATCAAGATGATCGCGGACTCAACCTGATTACGAGTTGTCCAAGTAGTCCAGGGTAAGAGAAACTGGAGAGA

The output :

573
 LCS is ACCGTTTGGTCAACACTCAACGCGCGACCGTAAAGATGGTTTTACAACCCCCTCCCTGTCCGGTTTATTTCTCTAGTCAGGACAAATAAAGGGGTGGGAATACTTTCTCGGACCAGACCACTACTGGTGGTTGACAAGAGTCGGCGGGGGAAAGGAAGGTTGTGTTAGTTATGGGCCGGAGCGAGGAGTTTGAGATCGAGTCTATTTGATTCGAATCACGGTTAACCTATGCACCTACTTGCCGATCCAGTGAACGACTATTGTACCATCGCGTAAAAAGGCTAGTCGATATCCTCCAAGCTAGTCTTCTGAGCTAAAAATTCGGGAGATCAAAAATATAAACTGACAGGTAAGCCTACGATCAGACCCCCCGCAAAATTGGAGTGTTTTGGCTAGAAAGAGCACCTTGAAACGGGCCTTTGTGGGTCCACACAGTTTCTGTAAGCTGTTCCACTACGGTCTTATGATCATCGGCAAGTTAGCACCACGCGAGGCGGACCCGGTATACTCCCCCCACGCTCGACATCTTGCGGCTCCTGATTAAGTTGTGTGTCGGGTAAAGAAATGGAGAGA

For LCS length finding standard LCS dynamic procedure is followed. It checks the values at that index. If they are equal then LCS length will increment, otherwise either the top or left value is copied. For constructing the LCS, start from the end and if the values at a certain index are equal then move to the diagonally opposite element and insert the character at the front of the lcs and the reverse process continues. It is advised to run a few short examples by hand to see why the algorithm works.


Related Solutions

Write a program that reads in a continuous stream of strings representing a line of CSV...
Write a program that reads in a continuous stream of strings representing a line of CSV data in the format "NAME,AGE,EMAIL,DOB". Implement a function check_csv which verifies that each input string matches this format by ensuring that: • There are 4 tokens in the string corresponding to the 4 properties above. • The second token MUST be a number. • The fourth token should be in the format MM-DD-YYYY (hint: tokenize this as well). The function should return 0 if...
Write a program in c that reads the content from the file and stores each line...
Write a program in c that reads the content from the file and stores each line in an int array in heap(using dynamic memory allocation). For example, let the file has elements following (we do not know the size of files, it could be above 100,000 and contents of the file and make sure to convert file elements to int): 10067 26789 6789 3467
2. Write a Java program that reads a series of input lines from given file “name.txt”,...
2. Write a Java program that reads a series of input lines from given file “name.txt”, and sorts them into alphabetical order, ignoring the case of words. The program should use the merge sort algorithm so that it efficiently sorts a large file. Contents of names.text Slater, KendallLavery, RyanChandler, Arabella "Babe"Chandler, StuartKane, EricaChandler, Adam JrSlater, ZachMontgomery, JacksonChandler, KrystalMartin, JamesMontgomery, BiancaCortlandt, PalmerDevane, AidanMadden, JoshHayward, DavidLavery,k JonathanSmythe, GreenleeCortlandt, OpalMcDermott, AnnieHenry, DiGrey, MariaEnglish, BrookeKeefer, JuliaMartin, JosephMontgomery, LilyDillon, AmandaColby, LizaStone, Mary FrancesChandler, ColbyFrye, DerekMontgomery,...
Write a program which reads an input file. It should assume that all values in the...
Write a program which reads an input file. It should assume that all values in the input file are integers written in decimal. Your program should read all integers from the file and print their sum, maximum value, minimum value, and average. Use the FileClient class here (from a previous reading) as an example. You'll need to create a file to be used as input to test your program, as well. Your program should work whether the integers are separated...
Write a C++ program that reads integers from standard input until end of file. Print out...
Write a C++ program that reads integers from standard input until end of file. Print out the largest integer that you read in, on a line by itself. Any erroneous input (something that is not an integer) should be detected and ignored. In the case where no integers are provided at all, print NO INTEGERS and stop. The whole program is under 40 lines of code. Just read from cin. Now, you need to detect non integers. In this case,...
Part 1 Write a program that reads a line of input and display the characters between...
Part 1 Write a program that reads a line of input and display the characters between the first two '*' characters. If no two '*' occur, the program should display a message about not finding two * characters. For example, if the user enters: 1abc*D2Efg_#!*345Higkl*mn+op*qr the program should display the following: D2Efg_#! 1) Name your program stars.c. 2) Assume input is no more than 1000 characters. 3) String library functions are NOT allowed in this program. 4) To read a...
Write a JAVA program that compares two strings input to see if they are the same?
Write a JAVA program that compares two strings input to see if they are the same?
Write a program that takes two sets ’A’ and ’B’ as input read from the file...
Write a program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the file...
Write a Python program that reads a file, input by the user, containing one word/token per...
Write a Python program that reads a file, input by the user, containing one word/token per line with an empty line between sentences. The program prints out the longest word found in the file along with its length.
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT