1
我需要帮助,以根据不同长度的字符串创建车牌(6个字符长度)。从不同但相似的字符串创建车牌
例1:
$str1 = "YE37";
$str2 = "TE37";
$str3 = "LYTE";
当我结合起来,它应该给我 “LYTE37”。我必须全部用它来制作一个盘子。我可以发现$ str1和$ str2之间的最长序列是“E37”,但不确定“Y”或“T”是否先出现(即“YTE37”还是“TYE37”)“,那么我可以与$ str3结合使用最长公共序列( “YTE”),这应该给我 “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;
}
我需要使用这些字符串,并创建一个车牌的算法。
@ Poiz,谢谢你的回答。它正在为上述两个例子工作。但对于“27RD”,“E27”,“AERD”来说,它不能很好地工作,因为它显示“RDEA27”,但实际上该板是“AE27RD”。我认为如果我们分析最长的共同序列并相应地组合,最好的办法是。 –