从不同但相似的字符串创建牌照

时间:2016-11-16 02:36:53

标签: php licensing

我需要帮助从不同的相等或不等长度的字符串创建一个牌照(6个字符长度)。

示例1:

$str1 = "YE37";
$str2 = "TE37";
$str3 = "LYTE";

当我结合时,它应该给我" LYTE37"。我必须用它们来制作一个盘子。我可以发现$ str1和$ str2之间常见的最长序列是" E37"但不确定" Y"或" T"首先出现(即,是否" YTE37"或" TYE37")"然后我可以使用最长的常用序列(" YTE")与$ str3结合使用,它应该给我" LYTE37"。

示例2:" YLF3"," EYLF"和" YLF37"应该给我" EYLF37"。

我使用以下函数找到最长的公共序列

$string_1="YE37";
$string_2="TE37";

$S =get_longest_common_subsequence($string_1, $string_2); // $S is "E37"


function get_longest_common_subsequence($string_1, $string_2)
    {
    $string_1_length = strlen($string_1);
    $string_2_length = strlen($string_2);
    $return          = '';

    if ($string_1_length === 0 || $string_2_length === 0)
    {
    // No similarities
    return $return;
    }

    $longest_common_subsequence = array();

    // Initialize the CSL array to assume there are no similarities
    $longest_common_subsequence = array_fill(0, $string_1_length, array_fill(0, $string_2_length, 0));

    $largest_size = 0;

    for ($i = 0; $i < $string_1_length; $i++)
    {
    for ($j = 0; $j < $string_2_length; $j++)
    {
    // Check every combination of characters
    if ($string_1[$i] === $string_2[$j])
    {
    // These are the same in both strings
    if ($i === 0 || $j === 0)
    {
    // It's the first character, so it's clearly only 1 character long
    $longest_common_subsequence[$i][$j] = 1;
    }
    else
    {
      // It's one character longer than the string from the previous character
    $longest_common_subsequence[$i][$j] = $longest_common_subsequence[$i - 1][$j - 1] + 1;
    }

    if ($longest_common_subsequence[$i][$j] > $largest_size)
    {
    // Remember this as the largest
    $largest_size = $longest_common_subsequence[$i][$j];
    // Wipe any previous results
    $return       = '';
    // And then fall through to remember this new value
    }

    if ($longest_common_subsequence[$i][$j] === $largest_size)
    {
    // Remember the largest string(s)
    $return = substr($string_1, $i - $largest_size + 1, $largest_size);
    }
    }
    // Else, $CSL should be set to 0, which it was already initialized   to
    }
    }

    // Return the list of matches
    return $return;
    }

我需要一种使用这些字符串并创建牌照的算法。

1 个答案:

答案 0 :(得分:-1)

  

这可能是您正在寻找的算法吗? Quick-Test Here

<?php       
    $str1 = "YE37";
    $str2 = "TE37";
    $str3 = "LYTE";

    $strA = "YLF3";
    $strB = "EYLF";
    $strC = "YLF37";

    function generatePlateNumber($str1, $str2, $str3) {
        $plateNumber    = '';
        $arr            = array($str1, $str2, $str3);
        $arrStr         = array();
        foreach($arr as $str){
            if(!preg_match("#\d#", $str)){
                $arrStr[]   = $str;
            }
        }
        foreach($arr as $str){
            if(preg_match("#\d#", $str)){
                $arrStr[]   = $str;
            }
        }
        $chars          = array_merge(str_split($arrStr[0]), 
                                      str_split($arrStr[1]), 
                                      str_split($arrStr[2]) );
        $alphabets      = [];
        $numbers        = [];
        foreach($chars as $char){
            if(is_numeric($char)){
                $numbers[]      = $char;
            }else{
                $alphabets[]    = $char;
            }
        }
        $alphabets  = array_unique($alphabets);
        $numbers    = array_unique($numbers);

        // BUILD THE PLATE NUMBER:
        $plateNumber .= implode($alphabets) . implode($numbers);
        return $plateNumber;
    }