Java LCS lcs(String s1, int s1min, int s1max, String s2, int s2min, int s2max)

Here you can find the source of lcs(String s1, int s1min, int s1max, String s2, int s2min, int s2max)

Description

lcs

License

Apache License

Declaration

private static final int lcs(String s1, int s1min, int s1max,
            String s2, int s2min, int s2max) 

Method Source Code

//package com.java2s;
/*//from w ww.  j  a v a  2  s.  co m
 * Copyright 2013 Adrien Favre-Bully, Florian Froese, Adrian Schmidmeister
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    private static final int lcs(String s1, int s1min, int s1max,
            String s2, int s2min, int s2max) {
        // stop criteria
        if (s1min < 0 || s1max > s1.length() || s1max - s1min < 1
                || s2min < 0 || s2max > s2.length() || s2max - s2min < 1) {
            return 0;
        }

        // dynamic programming: compute LCS in current region
        int array[][] = new int[s1max - s1min + 1][s2max - s2min + 1];

        for (int i = 1; i < array.length; i++) {
            for (int j = 1; j < array[i].length; j++) {
                if (s1.charAt(s1min + i - 1) == s2.charAt(s2min + j - 1)) {
                    array[i][j] = array[i - 1][j - 1] + 1;
                } else {
                    array[i][j] = Math
                            .max(array[i - 1][j], array[i][j - 1]);
                }
            }
        }

        // backtracking: compute bounds of found LCS
        int s1LCSmin = s1max;
        int s1LCSmax = s1min;
        int s2LCSmin = s2max;
        int s2LCSmax = s1min;

        int i = array.length - 1;
        int j = array[0].length - 1;

        while (i > 0 && j > 0) {
            if (s1.charAt(s1min + i - 1) == s2.charAt(s2min + j - 1)) {
                if (s1min + i - 1 < s1LCSmin) {
                    s1LCSmin = s1min + i - 1;
                }
                if (s1LCSmax < s1min + i - 1) {
                    s1LCSmax = s1min + i;
                }

                if (s2min + j - 1 < s2LCSmin) {
                    s2LCSmin = s2min + j - 1;
                }
                if (s2LCSmax < s2min + j - 1) {
                    s2LCSmax = s2min + j;
                }

                i--;
                j--;
            } else {
                if (array[i - 1][j] > array[i][j - 1]) {
                    i--;
                } else if (array[i - 1][j] < array[i][j - 1]) {
                    j--;
                } else {
                    i--;
                }
            }
        }

        if (array[array.length - 1][array[0].length - 1] == 0) {
            return 0;
        }

        return array[array.length - 1][array[0].length - 1] /* LCS length of current region         */
                + lcs(s1, s1min, s1LCSmin, s2, s2min, s2LCSmin) /* LCS length in left unmatched region   */
                + lcs(s1, s1LCSmax + 1, s1max, s2, s2LCSmax + 1, s2max); /* LCS length in right unmatched region   */
    }
}

Related

  1. lcs(String a, String b)
  2. LCS(String A, String B)
  3. lcs(String a, String b)
  4. lcs(String arg0, String arg1)
  5. LCS(String input1, String input2)
  6. lcs(String s1, String s2)
  7. lcs(String str1, String str2)
  8. lcs(String X, String Y, int m, int n)
  9. lcs1(int[] A, int[] B)