function longestCommonSubstring(str1, str2) {
    var n1 = str1.length;
    var n2 = str2.length;
    var cur = [],
        last = new Array(n1).fill(0),
        maxlen = 0,
        maxindex = 0;
    cur[-1] = 0;
    last[-1] = 0;
    for (var i = 0; i < n1; i++) {
        for (var j = 0; j < n2; j++) {
            if (str1[i] === str2[j]) {
                cur[j] = last[j - 1] + 1;
                if (cur[j] > maxlen) {
                    maxlen = cur[j];
                    maxindex = i + 1 - maxlen;
                }
            } else {
                cur[j] = last[j - 1];
            }
        }
        var t = last;
        last = cur;
        cur = last;
    }
    console.log("它们最长公共子串为 " + str1.substr(maxindex, maxlen));
    return maxlen;
}
var c = longestCommonSubstring("acbac", "acaccbabb");
console.log(c);
