Questions tagged [lcs]

lcs or Longest Common Subsequence is a problem in search optimization: Given two strings, find the common subsequence in given strings with maximum length. The problem can be solved in polynomial time using dynamic programming approach.

Source: http://en.wikipedia.org/wiki/Longest_common_subsequence_problem

About

lcs or Longest Common Subsequence is a problem in search optimization: Given two strings, find the common subsequence in given strings with maximum length. The problem can be solved in polynomial time using dynamic programming approach. The algorithm for solution to this problem is recursive and gives the following recursive formula.

   If i == 0 or j == 0                          then                           C[i][j] = 0
   If i,j > 0 and xi == yi                      then                           C[i][j] = c[i-1,j-1]+1
   If i,j > 0 and xi != yi                       then                           C[i][j] = max(c[i,j-1],c[i-1,j ])


Pseudocode

 p = A.length
 q = B.length
 for i = 1 to p
     c[i,0] = 0
 for i = 1 to q
     c[0,j] = 0
 for i = 1 to p
     for j = 1 to q
          if i ==0 or j == 0
              c[i][j] = 0
          else if(A[i] == B[j] ) 
              c[i][j] = c[i-1][j-1] + 1; 
          else 
              if(c[i][j-1]>c[i-1][j])
                   c[i][j] = c[i][j-1];
              else
                   c[i][j] = c[i-1][j];
  return c

Application

In Bioinformatics, the comparison of two DNA strands and similarity in these strands is given by this algorithm by computing longest common subsequence .


Example

  • A = A T G C G T C G A T
  • B = A T G T G A C T A G

LCS

Longest Common Subsequence is of 7 characters and it is A T G T G A T

204 questions
0
votes
0 answers

To make an array non-decreasing using dynamic programing

I came accross this question in a programming contest, i think it can be solved by DP but cannot think of any, so plz help. Here's the questn : There are n stack of coins placed linearly, each labelled from 1 to n. You also have a sack of coins…
0
votes
1 answer

How to find minimum no. of steps to make an array non-decreasing by selecting any interval and adding '1' to all items in the interval at each step?

Given an array, we need to find the minimum number of steps in which we can make it non decreasing. We can select i & j and add '1' to all the elements in the interval a[i] to a[j] (both inclusive) at each step for eg: A={3,2,1} answer is…
0
votes
1 answer

longest common consecutive subsequence

I know how to find the lcs of two sequences/strings, but lcs doesn't impose a restriction that the subsequence needs to be consecutive. I've tried it as follows function lccs(a, b) if a.length == 0 or b.length == 0 return "" possible…
bigblind
  • 12,539
  • 14
  • 68
  • 123
0
votes
2 answers

Longest Common Subsequence with openMP

I'm writing a parallel version of the Longest Common Subsequence algorithm using openMP. The sequential version is the following (and it works correctly): // Preparing first row and first column with zeros for(j=0; j < (len2+1); j++) score[0][j]…
Alberto Coletta
  • 1,563
  • 2
  • 15
  • 24
0
votes
1 answer

Report all long common subseqence(LCS) and its responsive location in the two strings

I just finished a program which enable to report all possible LCS and the location of the two strings respectively. However, the output of the test case isn't totally correct. For example, if the two strings are "AACADBCDADCB" and "DCACDCBBDBAD",…
0
votes
1 answer

Determining what is the subsequence in LCS algorithm

I've been studing for the last week the LCS problem, and I have a question. Whenever we are also interested in finding the longest subsequence itself (and not just its length), we create an auxiliary (string1.length X string2.length) matrix, to…
so.very.tired
  • 2,958
  • 4
  • 41
  • 69
0
votes
1 answer

longest common subsequence (restoring sequence)

I have 2 sequences and need to find longest common subsequence. Can't figure out why my function restore does not work. #sequenses A=[1,2,3] B=[2,3,1,5] #table AxB rows=[0]*(len(B)+1) table=[rows for l in range(len(A)+1)] for i in range(len(A)): …
Alice V.
  • 53
  • 1
  • 3
0
votes
2 answers

Diffing many texts against each other to derive template and data (finding common subsequences)

Suppose there are many texts that are known to be made from a single template (for example, many HTML pages, rendered from a template backed by data from some sort of database). A very simple example: id:937 name=alice; id:28 name=bob; id:925931…
GreyCat
  • 16,622
  • 18
  • 74
  • 112
0
votes
1 answer

Advice implementing LCS / SES in C++

I am implementing my own diff in c++ using the LCS algo. When looking at the LCS algo paper and trying to find the SES ( shortest edit script ) there are sometimes when you get a change for a line. In the following graph example it shows only…
bjackfly
  • 3,236
  • 2
  • 25
  • 38
0
votes
1 answer

Python string LCS

I am suppose to implement a version of LCS that does the following I/O: input: superLCS('cat','car') output: ['ca#','ca#'] currently, my program works for that, but if the letters are out of place, it doesnt work. for example, if the input is:…
user1681664
  • 1,771
  • 8
  • 28
  • 53
-1
votes
1 answer

Create a release pipeline in Azure DevOps which uploads a file to LCS, with a non admin user that doesn't have MFA

I'm trying to create a release pipeline in DevOps, that releases packages to LCS. The normal Dynamics 365 FO way of working. The issue is, I don't have an admin account without MFA that can be used to do this. Which roles or general setup, should I…
-1
votes
1 answer

Longest common subsequence on a PRAM model (complexity)

I'm trying to solve this exercise for this algorithm. I've tried to research on multithreading but I couldn't come up with a solution.
Nàdia
  • 1
  • 1
-1
votes
1 answer

Longest Common Substring (more than 2 arguments)

I have seen the solution for LCS 2 strings. Below is the code. I am curious how can I change it so that it can solve properly when more than 2 strings are given. I would appreciate any help or resource that can be useful Thank you. const printLCS =…
-1
votes
1 answer

I'm trying to write a code that take two files and compare them with the lcs algoritm. i can't understand why the program give me segmentation fault

//this is extlcs.c #include #include #include "extlcs.h" typedef struct { long long *row; long nRow; long long *col; long nCol; }ttable; int Max(int a, int b){ if(a>b) return a; else return…
-1
votes
2 answers

Longest Common Subsequent output wrong result

I've been trying to solve the LCS problem using recursion like below: #include #include using namespace std; string max(string x, string y) { return x.size() <= y.size() ? y : x; } string find(string x, string y) { //…
Hieu Na
  • 31
  • 5
1 2 3
13
14