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

Longest Common Subsequence with adjacent elements in k-window

Given two Strings and a window size k: Find the longest common subsequence with a constraint. The constraint is: adjacent elements in the common subsequence are within a k-window (The constraint ideally applies to both the input strings. But it's…
iwekesi
  • 2,228
  • 4
  • 19
  • 29
-1
votes
1 answer

Is LCS in Python just the intersection function in terms of length?

I was trying to otpimze a code that returns the length of the Longest Common Substring between two strings and realized that the insersection function returns pretty much the same. Am I correct? a = input() b = input() print…
-1
votes
1 answer

Longest Common Subsequence of Three Sequences of int

I am tring to solve the Longest Common Subsequence of Three Sequences of int using C++. the problem is a classic: Task. Given three sequences A = (a1, a2, . . . , an), B = (b1, b2, . . . , bm), and C = (c1, c2, . . . , cl), find the length of…
carnote
  • 13
  • 4
-1
votes
1 answer

Finding Number of Comparison in LCS In dynamic programming

#include #include int count=0; using namespace std; int max(int a,int b) { return (a>b)?a:b; } int lcs(char *x,char*y ,int m,int n) { int l[m+1][n+1]; int i,j; for( i=0;i<=m;i++) { …
-2
votes
2 answers

How to make sure that two strings only have certain alphabets in c++

Aim is to make sure that the user entered input for string 1 and string 2 contains only characters A,T,G or C in any order. If either string contains another other character then error should be displayed. Example: Input contains error Error in…
-2
votes
1 answer

What can I use instead of lcs (Longest common substring)

I am trying to calculate the common string using lcs, but this algorithm only calculates 1 string. What can I use instead? LCS = "aaabbbcccxxx" and "aaadddccc" result: "aaa" but what i want= "aaaccc" help please:)
qpkubi
  • 1
  • 1
-3
votes
2 answers

Longest repeating sequence using linq only

As the title says i have a task to find the longest repeating sequence in a string and it has to be done with linq only - no ifs, no loop, no try, assignment is only allowed on initialization of variables, recursion is allowed. I've found the…
Daniel
  • 1
  • 1
-3
votes
1 answer

Longest Common Subsequence in Ocaml

Does someone know how to find the longest common subsequence of a set of strings in Ocaml language?
Riccardo Queri
  • 1,025
  • 1
  • 10
  • 16
-4
votes
2 answers

Creating array with non constant sizes

I'm currently working on an assignment where I must find a way to output the longest common subsequence of two strings. In all of the other places I have found implementations of this code, they all share one similarity: multiple arrays are…
1 2 3
13
14