Question

In: Computer Science

C - programming problem: Let T be a sequence of an even length 2k, for some...

C - programming problem:

Let T be a sequence of an even length 2k, for some non-negative integer k. If its prefix of k symbols (or, the first k symbols in T) are the same as its suffix of k symbols (respectively, the last k symbols in T), then T is called a tandem sequence. For example, T[8] = 31103110 is a tandem sequence. Given a sequence S, if T is a subsequence of S and T is tandem, then T is called a tandem subsequence of S. One can then similarly define what a longest tandem subsequence (LTS for short) of S is.

Write a program that reads in two sequences with max length: 10,000, which computes, and prints the longest-tandem subsequence or LTS, and its length.

Sample program run:

Enter a sequence: 3110283025318292818318143

# an LTS (length = 10) for the sequence is:

1283312833

Solutions

Expert Solution

Complete Program:




Sample Output:

CODE TO COPY:

// Header files section
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// constant declaration
#define MAX 10000

// getLCS function returns the
// longest common subsequence of two sequences
char* getLCS(char* s1, char* s2, int m, int n)
{
    int** L = (int**)malloc((m + 1) * sizeof(int*));
    for (int i = 0; i < (m + 1); i++)
    {
        L[i] = (int*)malloc((n + 1) * sizeof(int));
    }

    for (int i = 0; i <= m; i++)
    {
        for (int j = 0; j <= n; j++)
        {
            if (i == 0 || j == 0)
            {
                L[i][j] = 0;
            }
            else if (s1[i - 1] == s2[j - 1])
            {
                L[i][j] = L[i - 1][j - 1] + 1;
            }
            else
            {
                L[i][j] = max(L[i - 1][j], L[i][j - 1]);
            }
        }
    }

    int p = m;
    int q = n;
    int r = L[m][n];
    char* lcs = (char*)malloc(sizeof(char) * (r + 1));
    lcs[r] = '\0';
  
    while (p > 0 && q > 0)
    {
        if (s1[p - 1] == s2[q - 1])
        {
            lcs[r - 1] = s1[p - 1];
            p--;
            q--;
            r--;
        }
        else if (L[p - 1][q] > L[p][q - 1])
        {
            p--;
        }
        else
        {
            q--;
        }
    }

    return lcs;
} // end of getLCS function

// findLTS function returns the
// longest tandem subsequence of a sequence
char* findLTS(char* S)
{
    int m = 1;
    int n = strlen(S) - 1;
    int len = 0;
    char term[MAX] = "";

    do
    {
        char s1[MAX];
        char s2[MAX];
        int i, j;

        for (i = 0; i < m; i++)
        {
            s1[i] = S[i];
        }
        s1[i] = '\0';

        for (j = 0; j < n; j++, i++)
        {
            s2[j] = S[i];
        }
        s2[j] = '\0';

        // call the getLCS function
        char* temp = getLCS(s1, s2, m, n);
        int rec = strlen(temp);

        if (rec >= len)
        {
            len = rec;
            strcpy(term, temp);
        }

        m++;
        n--;
    } while (m < n);

    char T[MAX] = "";  
    strcat(T, term);
    strcat(T, term);

    return T;
} // end of findLTS function

// start main function
int main()
{
    // prompt the user for a sequence
    char S[MAX];
    printf("Enter a sequence: ");
    scanf("%s", S);  

    // call the findLTS function
    char* T = findLTS(S);
  
    // print the LTS and its size
    printf("\n# an LTS (length = %d) for the sequence is:\n", strlen(T));
    printf("%s\n", T);

    return 0;
} // end of main function


Related Solutions

(a) Let n = 2k be an even integer. Show that x = rk is an...
(a) Let n = 2k be an even integer. Show that x = rk is an element of order 2 which commutes with every element of Dn. (b) Let n = 2k be an even integer. Show that x = rk is the unique non-identity element which commutes with every element of Dn. (c) If n is an odd integer, show that the identity is the only element of Dn which commutes with every element of Dn.
Using C++ use dynamic programming to list first 30 Fibonacci numbers. Fibonacci sequence is famous problem...
Using C++ use dynamic programming to list first 30 Fibonacci numbers. Fibonacci sequence is famous problem solved with recursion. However, this can also be done more efficiently using dynamic programming. Create a program that uses dynamic programming techniques to list the first 30 Fibonacci numbers.
Let C be a plane curve parameterized by arc length by α(s), T(s) its unit tangent...
Let C be a plane curve parameterized by arc length by α(s), T(s) its unit tangent vector and N(s) be its unit normal vector. Show d dsN(s) = −κ(s)T(s).
2. Let {Zt , t = 0, ±1, ±2, ...} be a sequence of independent random...
2. Let {Zt , t = 0, ±1, ±2, ...} be a sequence of independent random variables, each with mean EZt = 0 and variance Var(Zt) = σ 2 . Define Xt = ZtZt−1 + Zt−2. • Compute the mean and the covariance function for Xt . • Is {Xt} weakly stationary? Explain why.
A DNA string is a sequence of the bases a, c, g, and t in any...
A DNA string is a sequence of the bases a, c, g, and t in any order, whose length is usually a multiple of three. In reality, it is not necessarily a multiple of three, but we will simplify it as such for discussion. For example, aacgtttgtaaccagaactgt is a DNA string with a length of 21 bases. Recall that a sequence of three consecutive letters is called a codon. Assuming the first codon starts at position 1, the codons are...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing chess himself to practice his abilities. The chess that Jojo played was N × N. When Jojo was practicing, Jojo suddenly saw a position on his chessboard that was so interesting that Jojo tried to put the pieces of Rook, Bishop and Knight in that position. Every time he put a piece, Jojo counts how many other pieces on the chessboard can be captured...
Biologists use a sequence of the letters A, C, T, and G to model a genome....
Biologists use a sequence of the letters A, C, T, and G to model a genome. A gene is a substring of a genome that starts after a triplet ATG and ends before a triplet TAG, TAA, or TGA. Furthermore, the length of a gene string is a multiple of 3, and the gene does not contain any of the triplets ATG, TAG, TAA, or TGA. Write a program that prompts the user to enter a genome and displays all...
C programming problem < purpose: need to find time and space complexities in this problem and...
C programming problem < purpose: need to find time and space complexities in this problem and which function(algorithm) will be used in this problem> I have to design an efficient algorithm which solves this problem. Also, it needs to include time and space complexities of this algorithm. There is a matrix (N times N) of integers which rows and columns are sorted in non-decreasing order. A sorted matrix and integer M will be given and we need to find the...
Let S be the square centered at the origin with sides of length 2, and C...
Let S be the square centered at the origin with sides of length 2, and C be the unit circle centered at the origin. (a) If you randomly throw a point on S, what is the probability that it will lie in C? Ans: 0.785 (b) Describe how you could use simulation to estimate the probability in part (a). (c) How can you use simulation to estimate a? For part b and c, there maybe a need to generate random...
The purpose of this C++ programming assignment is to practice using an array. This problem is...
The purpose of this C++ programming assignment is to practice using an array. This problem is selected from the online contest problem archive, which is used mostly by college students worldwide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest. For your convenience, I copied the description of the problem below with my note on the I/O and a sample executable. Background The world-known gangster Vito Deadstone...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT