Java中最短的常见超字符串

时间:2018-08-27 08:07:16

标签: java

我有一个来自互联网的程序

Shortest Common Super String

程序给出正确的输出,但是当我在Java中转换程序时,它没有给出相同的输出。我在下面粘贴了我的Java程序

public class ShortestCommonSuperString {

    private static int min(int a, int b) {
        return (a < b) ? a : b;
    }

    // Function to calculate maximum overlap in two given strings
    private static int findOverlappingPair(String str1, String str2, RefObjectOne<String> str) {
        int max = -2147483648;
        int len1 = str1.length();
        int len2 = str2.length();

        for (int i = 1; i <= min(len1, len2); i++) {
            // compare last i characters in str1 with first i
            // characters in str2
            if (str1.regionMatches(0, str2, str2.length() - i - 1, i) == false) {
                if (max < i) {
                    max = i;
                    str.argValue = str1 + str2.substring(i);
                }
            }
        }

        // check prefix of str1 matches with suffix of str2
        for (int i = 1; i <= min(len1, len2); i++) {
            // compare first i characters in str1 with last i
            // characters in str2
            if (str1.regionMatches(str1.length() - i - 1, str2, 0, i) == false) {
                if (max < i) {
                    //update max and str
                    max = i;
                    str.argValue = str2 + str1.substring(i);
                }
            }
        }

        return max;
    }

    private static String findShortestSuperstring(String[] arr, int len) {
        // run len-1 times to consider every pair
        while (len != 1) {
            System.out.println("Wokring While");
            int max = -2147483648; // to store maximum overlap
            int l = 0; // to store array index of strings
            int r = 0;
            // involved in maximum overlap
            String resStr = null; // to store resultant string after
            String str = null;
            // maximum overlap

            for (int i = 0; i < len; i++) {
                System.out.println("Working For ONE");
                for (int j = i + 1; j < len; j++) {
                    System.out.println("Working For TWO");
                    RefObjectOne<String> tempRef_str = new RefObjectOne<String>(str);
                    int res = findOverlappingPair(arr[i], arr[j], tempRef_str);
                    str = tempRef_str.argValue;
                    System.out.println(str);
                    // check for maximum overlap
                    if (max < res) {
                        max = res;
                        resStr = str;
                        l = i;
                        r = j;
                    }
                }
            }

            len--; //ignore last element in next cycle

            // if no overlap, append arr[len] to arr[0]
            if (max == -2147483648) {
                arr[0] += arr[len];
            } else {
                arr[l] = resStr; // copy resultant string to index l
                arr[r] = arr[len]; // copy string at last index to index r
            }
        }
        return arr[0];
    }

    // Driver program
    public static void main(String[] args) {
        String[] arr = {"catgc", "ctaagt", "gcta", "ttca", "atgcatc"};
        int len = arr.length;
        System.out.println("The shortest super string is" + findShortestSuperstring(arr, len));
    }
}

class RefObjectOne<T> {
    public T argValue;

    public RefObjectOne(T refArg) {
        this.argValue = refArg;
    }
}

我得到的输出是这样的->最短的超字符串catgctc 这不是正确的输出。无法弄清楚我在哪里做错

1 个答案:

答案 0 :(得分:0)

您必须对regionMatches()使用适当的参数。这是正确的:

// Function to calculate maximum overlap in two given strings
private static int findOverlappingPair(String str1, String str2, RefObjectOne<String> str) {
    int max = -2147483648;
    int len1 = str1.length();
    int len2 = str2.length();

    for (int i = 1; i <= min(len1, len2); i++) {
        // compare last i characters in str1 with first i
        // characters in str2
        if (str1.regionMatches(len1 - i, str2, 0, i)) {
            if (max < i) {
                max = i;
                str.argValue = str1 + str2.substring(i);
            }
        }
    }

    // check prefix of str1 matches with suffix of str2
    for (int i = 1; i <= min(len1, len2); i++) {
        // compare first i characters in str1 with last i
        // characters in str2
        if (str1.regionMatches(0, str2, len2 - i, i)) {
            if (max < i) {
                // update max and str
                max = i;
                str.argValue = str2 + str1.substring(i);
            }
        }
    }

    return max;
}