给定数组中最长的重复数组元素子集

时间:2013-07-27 09:58:01

标签: javascript jquery

您好我有关于jquery的问题,我需要找到给定数组中最长的重复子集。

示例:

my_array['b','r','o','w','n','f','o','x','h','u','n','t','e','r','n','f','o','x','r','y','h','u','n']

结果应该是nfox。 我有以下代码:

 string = my_array.join('');
 for(i=0; i < my_array.length; i++)
 {

  for(j=0; j < my_array.length; j++)
 {

 string.substring(Math.abs(j-i));
 }

 }

但它似乎没有像我想的那样工作,也许我错过了一些jquery函数?

4 个答案:

答案 0 :(得分:3)

循环遍历数组,然后在内部循环中搜索所有可能的子字符串长度。最大重新发生的子串最多只能是阵列长度的一半。

使用indexOfindexLastOf函数搜索重新出现的子字符串。如果两个函数都找到子字符串并且找到的位置不同,则重新发生子字符串。

my_array = ['b','r','o','w','n','f','o','x','h','u','n','t','e','r','n','f','o','x','r','y','h','u','n'];
    str = my_array.join('');

    var greatestLen = 0;
    var highestPosn1 = -1;
    var highestPosn2 = -1;

    for (var n = 0; n < my_array.length; ++n)
    {
        for (var l = 1; l <= my_array.length/2; ++l)
        {
            var subs = str.substr(n,l);
            var find1 = str.indexOf(subs);
            var find2 = str.lastIndexOf(subs);
            if (find1 != -1 && find2 != -1 && find1 != find2)
            {
                var longestSubString = subs;
                if (longestSubString.length > greatestLen)
                {
                    highestPosn1 = find1;
                    highestPosn2 = find2;
                    greatestLen = longestSubString.length;
                }
            }
        }
    }
    console.info('Longest substr ' + greatestLen + ' at posn1=' + highestPosn1 + ' and posn2=' + highestPosn2);

答案 1 :(得分:0)

这是我用php设法做的事情

function longest_subset ($input = array()) {
$longestSubstring = "";
$inputString =implode("",$input);
echo $inputString;
if (!is_array($input)) {
throw new InvalidArgumentException("Invalid Input Type", 1);
}
if (empty($input)) {
throw new LengthException("Your array must contain one or more values", 1);
}

for( $i=0; $i < strlen($inputString); $i++) {
for( $j=0; $j < strlen($inputString); $j++) {
$length = abs($j-$i);
echo $length.'<br>';

$substring = substr($inputString, $i, $length);
//echo $substring.'<br>';

$doesSubstringRepeat = strrpos($inputString,$substring) > $i;
$substringLongerThanLongest = strlen($substring) > strlen($longestSubstring);

if ($doesSubstringRepeat && $substringLongerThanLongest) {
$longestSubstring = $substring;
}
}
}

return strlen($longestSubstring) > 0 ? str_split($longestSubstring) : false;
}

答案 2 :(得分:0)

In JavaScript。如果你包含重叠的字符串,你实际上可以拥有超过原始长度一半的最大重复子字符串:字符串“abababababab4”在索引0和2处有字符串“ababababab”

// For some fancy printing
function post(string) {
    document.getElementById("stati").innerHTML += string;
}

var myarray = ['b','r','o','w','n',
               'f','o','x',
               'h','u','n','t','e','r',
               'n','f','o','x',
               'r','y','h','u','n'];
var string = myarray.join("");
var longestRepeated = "";

// i = current size of substring
// i loop runs string.length times;
// looks for large repeated substrings first
for (i = string.length; i > 0; i--) {
    post("<code>Searching all substrings of length " + i + "</code><br>");

    // j = current beginning index of substring
    // j loop runs (string.length - substring.length + 1) times;
    // checks for repeat of subStr in
    // range (indexOf(subStr), whileCurrentSizeiStillFitsInString]
    for (j = 0; j < string.length - i; j++) {
        var subStr = string.substring(j, j + i);
        post("<code class='a'>at index " + j + ", substring = '" +
              subStr + "'</code><br>");

        // if subStr is matched between indexOf(subStr) + 1 and
        // the end of the string, we're done here
        // (I'm not checking for multiple repeated substrings
        //  of the same length. Get over it.)
        if (string.indexOf(subStr, j + 1) != -1) {
            longestRepeated = subStr;
            break;
        }
    }
    if (longestRepeated.length) break;
}

if (longestRepeated.length) alert("Longest repeated substring: " +
                                   subStr);
else alert("Longest repeated substring is the whole string.");

答案 3 :(得分:0)

万一仍然有人需要它,这是一个更通用的解决方案,它不关心数组内容,并考虑到元素的类型返回最长的重复子集。

function solve(array $list): array
{
    $length = count($list);
    $longestRepeatedSequenceLength = intdiv($length + 1, 2);

    while ($longestRepeatedSequenceLength > 0) {
        $possibleIterations = intdiv($length, $longestRepeatedSequenceLength);

        for ($i = 0; $i < $possibleIterations; $i++) {
            $offset = $longestRepeatedSequenceLength + $i;

            if ($length - $offset < 0) {
                continue;
            }

            $subset = array_slice($list, $i, $longestRepeatedSequenceLength);

            // take overlapping strings in consideration
            $testIn = ($longestRepeatedSequenceLength > 1) ?
                array_slice($list, $offset - 1, $length - $offset + 1) :
                array_slice($list, $offset, $length - $offset);

            if (isSubset($testIn, $subset)) {
                return $subset;
            }
        }

        $longestRepeatedSequenceLength--;
    }

    return [];
}

此解决方案考虑了重叠的子集,因此您可以对其进行编辑,这样就不必考虑仅通过替换简写的if语句(在注释下)通过此行:

$testIn = array_slice($list, $offset, $length - $offset);

您在这里:

function isSubset(array $arr, array $subArray): bool
{
    if (count($arr) < count($subArray)) {
        return false;
    }

    $keys = array_keys($arr, $subArray[array_keys($subArray)[0]]);

    foreach ($keys as $k) {
        if (array_slice($arr, $k, count($subArray)) === $subArray) {
            return true;
        }
    }

    return false;
}