<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Algorithm &amp;quot;Longest common subsequence problem&amp;quot; in ABAP in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/algorithm-quot-longest-common-subsequence-problem-quot-in-abap/m-p/688255#M30885</link>
    <description>&lt;P&gt; &lt;/P&gt;
  &lt;P&gt;Hi,&lt;/P&gt;
  &lt;P&gt; &lt;/P&gt;
  &lt;P&gt;I'm a little lazy to develop it myself... Did anyone implement the algorithm "&lt;A href="https://en.wikipedia.org/wiki/Longest_common_subsequence_problem"&gt;Longest common subsequence problem&lt;/A&gt;" between 2 strings, in ABAP, and would like to share it? This is an algorithm which can be used in Git to display the characters which are different between two lines for instance.&lt;/P&gt;
  &lt;P&gt; &lt;/P&gt;
  &lt;P&gt;I want to achieve something like that (Azure DevOps), i.e. 2 colors on the same line (light green versus darker green) - My issue is to know which characters are different, not how to render the colors of course:&lt;/P&gt;
  &lt;P&gt; &lt;/P&gt;
  &lt;P&gt; &lt;/P&gt;
  &lt;P&gt; &lt;/P&gt;
  &lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/230340-aajx0.png" /&gt;&lt;/P&gt;
  &lt;P&gt; &lt;/P&gt;
  &lt;P&gt; &lt;/P&gt;
  &lt;P&gt;Or do you know a better alternative?&lt;/P&gt;
  &lt;P&gt; &lt;/P&gt;
  &lt;P&gt;Thanks &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
  &lt;P&gt; &lt;/P&gt;
  &lt;P&gt;Sandra&lt;/P&gt;</description>
    <pubDate>Tue, 02 Oct 2018 15:02:04 GMT</pubDate>
    <dc:creator>Sandra_Rossi</dc:creator>
    <dc:date>2018-10-02T15:02:04Z</dc:date>
    <item>
      <title>Algorithm "Longest common subsequence problem" in ABAP</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/algorithm-quot-longest-common-subsequence-problem-quot-in-abap/m-p/688255#M30885</link>
      <description>&lt;P&gt; &lt;/P&gt;
  &lt;P&gt;Hi,&lt;/P&gt;
  &lt;P&gt; &lt;/P&gt;
  &lt;P&gt;I'm a little lazy to develop it myself... Did anyone implement the algorithm "&lt;A href="https://en.wikipedia.org/wiki/Longest_common_subsequence_problem"&gt;Longest common subsequence problem&lt;/A&gt;" between 2 strings, in ABAP, and would like to share it? This is an algorithm which can be used in Git to display the characters which are different between two lines for instance.&lt;/P&gt;
  &lt;P&gt; &lt;/P&gt;
  &lt;P&gt;I want to achieve something like that (Azure DevOps), i.e. 2 colors on the same line (light green versus darker green) - My issue is to know which characters are different, not how to render the colors of course:&lt;/P&gt;
  &lt;P&gt; &lt;/P&gt;
  &lt;P&gt; &lt;/P&gt;
  &lt;P&gt; &lt;/P&gt;
  &lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/230340-aajx0.png" /&gt;&lt;/P&gt;
  &lt;P&gt; &lt;/P&gt;
  &lt;P&gt; &lt;/P&gt;
  &lt;P&gt;Or do you know a better alternative?&lt;/P&gt;
  &lt;P&gt; &lt;/P&gt;
  &lt;P&gt;Thanks &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
  &lt;P&gt; &lt;/P&gt;
  &lt;P&gt;Sandra&lt;/P&gt;</description>
      <pubDate>Tue, 02 Oct 2018 15:02:04 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/algorithm-quot-longest-common-subsequence-problem-quot-in-abap/m-p/688255#M30885</guid>
      <dc:creator>Sandra_Rossi</dc:creator>
      <dc:date>2018-10-02T15:02:04Z</dc:date>
    </item>
    <item>
      <title>Re: Algorithm "Longest common subsequence problem" in ABAP</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/algorithm-quot-longest-common-subsequence-problem-quot-in-abap/m-p/688256#M30886</link>
      <description>&lt;P&gt;I'm not so lazy in fact &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; - Here is a basic solution, not much tested, use it at your own risk (and from there, it's easy to implement the colors - note that there can be different algorithms for the diff, more intelligent like comparison based on words) :&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;CLASS lcl_lcs DEFINITION.
  PUBLIC SECTION.

    METHODS constructor
      IMPORTING
        x TYPE string
        y TYPE string.

    DATA: result TYPE string READ-ONLY.

  PRIVATE SECTION.

    METHODS lcs_length
      RETURNING
        VALUE(lcs_length) TYPE i.

    METHODS backtrack
      IMPORTING
        i TYPE i
        j TYPE i.

    DATA: x     TYPE REF TO string,
          y     TYPE REF TO string,
          c     TYPE TABLE OF i,
          m     TYPE i,
          n     TYPE i,
          dim_j TYPE i,
          dim_c TYPE i.

ENDCLASS.

CLASS lcl_lcs IMPLEMENTATION.

  METHOD constructor.

    me-&amp;gt;x = REF #( x ).
    me-&amp;gt;y = REF #( y ).
    m = strlen( x ).
    n = strlen( y ).
    dim_j = strlen( y ) + 1.
    dim_c = ( m + 1 ) * ( n + 1 ).

    " add lines by using "factorial" (may overflow by 2, more memory but it's fast)
    APPEND INITIAL LINE TO c.
    DO.
      APPEND LINES OF c TO c.
      IF lines( c ) &amp;gt;= dim_c.
        EXIT.
      ENDIF.
    ENDDO.

    lcs_length( ).

    backtrack( i = ( m - 1 ) j = ( n - 1 ) ).

    FREE c.

  ENDMETHOD.

  METHOD lcs_length.
*function LCSLength(X[1..m], Y[1..n])
*    C = array(0..m, 0..n)
*    for i := 0..m
*       C[i,0] = 0
*    for j := 0..n
*       C[0,j] = 0
*    for i := 1..m
*        for j := 1..n
*            if X[i] = Y[j]
*                C[i,j] := C[i-1,j-1] + 1
*            else
*                C[i,j] := max(C[i,j-1], C[i-1,j])
*    return C[m,n]
*Alternatively, memoization could be used.

    DO m TIMES.
      DATA(i) = sy-index.
      DATA(im) = sy-index - 1.
      DO n TIMES.
        DATA(j) = sy-index.
        DATA(jm) = sy-index - 1.
        IF x-&amp;gt;*+im(1) = y-&amp;gt;*+jm(1).
          c[ dim_j * i + j + 1 ] = c[ dim_j * im + jm + 1 ] + 1.
        ELSE.
          c[ dim_j * i + j + 1 ] = nmax( val1 = c[ dim_j * i + jm + 1 ] val2 = c[ dim_j * im + j + 1 ] ).
        ENDIF.
      ENDDO.
    ENDDO.

    lcs_length = c[ dim_j * m + n + 1 ].

  ENDMETHOD.

  METHOD backtrack.
*Reading out a LCS
*The following function backtracks the choices taken when computing the C table. If the
*last characters in the prefixes are equal, they must be in an LCS. If not, check what
*gave the largest LCS of keeping {\displaystyle x_{i}} x_{i} and {\displaystyle y_{j}} y_{j},
*and make the same choice. Just choose one if they were equally long. Call the function with i=m and j=n.

*function backtrack(C[0..m,0..n], X[1..m], Y[1..n], i, j)
*    if i = 0 or j = 0
*        return ""
*    if  X[i] = Y[j]
*        return backtrack(C, X, Y, i-1, j-1) + X[i]
*    if C[i,j-1] &amp;gt; C[i-1,j]
*        return backtrack(C, X, Y, i, j-1)
*    return backtrack(C, X, Y, i-1, j)

    IF i = -1 OR j = -1.
      " nothing (stop the recursivity)
    ELSEIF x-&amp;gt;*+i(1) = y-&amp;gt;*+j(1).
      backtrack( i = i - 1 j = j - 1 ).
      result = result &amp;amp;&amp;amp; x-&amp;gt;*+i(1).
    ELSEIF c[ dim_j * ( i + 1 ) + ( j ) + 1 ] &amp;gt; c[ dim_j * ( i ) + ( j + 1 ) + 1 ].
      backtrack( i = i j = j - 1 ).
    ELSE.
      backtrack( i = i - 1 j = j ).
    ENDIF.

  ENDMETHOD.

ENDCLASS.

CLASS ltc_main DEFINITION
      FOR TESTING
      DURATION SHORT
      RISK LEVEL HARMLESS
      INHERITING FROM cl_aunit_assert.
  PRIVATE SECTION.
    METHODS test FOR TESTING.
ENDCLASS.

CLASS ltc_main IMPLEMENTATION.
  METHOD test.
    cl_abap_unit_assert=&amp;gt;assert_equals( act = NEW lcl_lcs( x = `XMJYAUZ` y = `MZJAWXU` )-&amp;gt;result exp = `MJAU` ).
  ENDMETHOD.
ENDCLASS.&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Oct 2018 16:51:34 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/algorithm-quot-longest-common-subsequence-problem-quot-in-abap/m-p/688256#M30886</guid>
      <dc:creator>Sandra_Rossi</dc:creator>
      <dc:date>2018-10-02T16:51:34Z</dc:date>
    </item>
  </channel>
</rss>

