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
1 answer

AttributeError: 'list' object has no attribute '_all_hypernyms' what is this error?

This program is to find similarities between the a sentences and words and how they are similar in synonyms I have downloaded the nltk when i first coded it was run and there were no errors but after some days when i run the program ti give me this…
mir shahab
  • 45
  • 7
0
votes
0 answers

longest common substring k gap

Longest Common String with one Gap allowed So I want to find out the longest common string among the two strings for eg AABA and AAGA such that it can ignore one char that is B and G to give longest common substring as AAA I tried the logic using…
0
votes
0 answers

How can we calculate longest common substring with one atmost one skip?

Here is the code: I am able to find the longest common substring but I would like to find the longest common substring with at most one skip ? #include using namespace std; string X,Y; int lcs(int i, int j, int count) { if (i…
0
votes
0 answers

Parallel LCS computation using diagonal aproach

I am trying to calculate LCS length using OpenMP. I picked up a conference paper on it and started executing the algorithm. To be honest I didn't understand how the algorithm works to very detail, but understood that it was trying to calculate the…
Kushal Mondal
  • 93
  • 1
  • 8
0
votes
1 answer

How to calculate longest common substring anywhere in two strings

I am trying to calculate the longest exact common substring without gaps between a string and a vector of strings in R. How do I modify stringdist to return any common string anywhere in the two compared strings and return the distance? Reproduce…
Neal Barsch
  • 2,810
  • 2
  • 13
  • 39
0
votes
1 answer

Recursion showing error for finding longest common sub-sequence in python

This is the ideone link. I want to solve the famous lcs problem by dynamic programming, but can't figure this error out. It says as following when I run this function: TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' a =…
Jonty K.
  • 7
  • 1
  • 5
0
votes
0 answers

My code to find the LCS is returning a null matrix

I'm making a code to find the longest common subsequence between two strings, which creates a matrix for this purpose. The idea is that for each position [i][j], you define the value of that position as the greatest sum(or path) until that way and…
francescobfc
  • 107
  • 1
  • 11
0
votes
1 answer

Get Longest Common SubString from a list of sentences

Given the following lines "Alberry K2503 F40 D", "Alberry K2503 F40 S", "Demi Deco Denver BLK", "Demi Deco Denver BRN", "Demi Deco Tank", "Demi Deco…
Eugen
  • 2,934
  • 2
  • 26
  • 47
0
votes
1 answer

Recursion: keeping track of a variable in all recursion paths when multiple sub paths are possible

I am trying to count how many times a pattern occurs as a subsequence of a string and also keep the indices where the match happens. The counting is easy using recursive calls. function count(str, pattern, strInd, patternInd) { if (patternInd…
gaurav5430
  • 12,934
  • 6
  • 54
  • 111
0
votes
0 answers

Optimized solution for longest common subsequence

Which algorithm is the most optimized and efficient for finding the longest common subsequence (strings longer than 400 characters)?
mortal21
  • 19
  • 3
0
votes
0 answers

Initialize two 2D arrays and fill the first column and first row of one of the arrays with 0

This function should initialize arrays C[m+1][n+1] and B[m][n] and fill the first row and first column of C with zeros. Note: int*** C refers to a pointer points to the 2D integer array. Please correct the error. void initLCSTable(int*** C, char***…
0
votes
0 answers

Initialize two 2D arrays and fill the first row and first column of array C with 0s.

This function initializes arrays C[m+1][n+1] and B[m][n] and fill the first row and first column of C with zeros. Note: int*** C refers to a pointer points to the 2D integer array example: https://i.stack.imgur.com/ElWMY.png void initLCSTable(int***…
Ayaan
  • 45
  • 1
  • 5
0
votes
1 answer

Longest Common Subsequence C (Python Script explanation)

i have been working for the last few days on a Longest Common Subsequence program, in C, using dynamic programming. Though, I have a memory issue as i am trying to process a lot of data (and i mean A LOT) which result to memory overflow. Fortunately…
0
votes
1 answer

LCS(longest common subsequence) in Scheme

I try to use Scheme to implement LCS algorithm, but there is a bug. (define X (list #\A #\B #\C #\B #\D #\A #\B)) (define Y (list #\B #\D #\C #\A #\B #\A)) (define prefix (lambda (i s) (if (= i 0) '() (cons (car s) …
TianpingHsu
  • 91
  • 2
  • 5
0
votes
0 answers

Fuzzy String Search using Longest Common Substring for One Vector using R

What is a good approach to tackle a single vector with 2 million data to achieve the following outcome:- Getting the top "X" number of Longest Common Substring matches Frequency of occurrence of item 1. Example of a smaller set of…