使用 PHP 从数组和给定模式中获取最接近的序列结果

Get nearest sequence result from an array and given pattern with PHP

提问人:devjs11 提问时间:5/27/2020 最后编辑:devjs11 更新时间:5/27/2020 访问量:75

问:

我正在尝试使用既定的顺序从字母中获取年份和月份。我知道该序列基于以下字母:

$letters = array('B','C','D','F','G','H','J','K','L','M','N','P','R','S','T','V','W','X','Y','Z');

它从 0000BBB 开始,当它达到 9999 时,它变成了 BBC、BBD 等。因此,在这种情况下,我不需要数字,只需要字母,因为我每年和每月都有最后注册的序列列表,如下所示:

$plates = array(
            array('2018','KHF','KHX','KJV','KKN','KLM','KML','KNK','KPD','KPR','KPT','----','----'),
            array('2017','JWN','JXF','JYB','JYT','JZP','KBM','KCH','KCV','KDK','KFB','KFV','KGN'),
            array('2016','JLN','JMF','JMY','JNR','JPK','JRG','JRZ','JSL','JTB','JTR','JVH','JVZ'),
            array('2015','JCK','JCY','JDR','JFG','JFW','JGP','JHJ','JHT','JJH','JJW','JKK','JKZ'),
            array('2014','HVN','HVZ','HWM','HXB','HXN','HYD','HTY','HZB','HZL','HZZ','JBL','JBY'),
            array('2013','HNT','HPC','HPN','HPY','HRK','HRX','HSK','HSR','HSZ','HTK','HTV','HVF'),
            array('2012','HJC','HJM','HKB','HKL','HKX','HLK','HLW','HMD','HML','HMT','HNC','HNK'),
            array('2011','HBP','HCB','HCR','HDC','HDR','HFF','HFT','HGC','HGM','HGX','HHH','HHT'),
            array('2010','GTC','GTS','GVM','GWC','GWV','GXP','GYD','GYM','GYX','GZJ','GZT','HBG'),
            array('2009','GKS','GLC','GLP','GMC','GMN','GNF','GNY','GPJ','GPW','GRM','GSC','GSR'),
            array('2008','FZR','GBN','GCK','GDH','GFC','GFY','GGV','GHG','GHT','GJJ','GJV','GKH'),
            array('2007','FKY','FLV','FNB','FNZ','FRC','FSJ','FTP','FVJ','FWC','FXB','FXY','FYY'),
            array('2006','DVW','DWT','DXZ','DYY','FBC','FCJ','FDP','FFK','FGF','FHD','FJD','FKC'),
            array('2005','DFZ','DGX','DHZ','DKB','DLD','DMJ','DNP','DPK','DRG','DSC','DTB','DVB'),
            array('2004','CRV','CSS','CTT','CVR','CWR','CXT','CYY','CZP','DBJ','DCH','DDG','DFF'),
            array('2003','CDV','CFM','CGJ','CHF','CJC','CKB','CLD','CLV','CMM','CNK','CPF','CRC'),
            array('2002','BSL','BTF','BTZ','BVW','BWT','BXP','BYP','BZF','BZV','CBP','CCH','CDC'),
            array('2001','BFJ','BGF','BHG','BJC','BKB','BLC','BMF','BMW','BNL','BPG','BRB','BRT'),
            array('2000','---','---','---','---','---','---','---','---','BBJ','BCD','BCY','BDR')
        );

这意味着数组索引 0 是年份,从 1 到 12 是月份。我正在尝试找到匹配项,但后来意识到我无法搜索确切的值,需要根据字母查找最接近的值。

如果有人能指导我朝着正确的方向前进,我将不胜感激,最好的方法是什么。

到目前为止,这是一个测试,但这只会返回一个完全匹配,我必须搜索任何可能的字母,例如 KHW 作为示例,这些字母必须与 KHX 最接近的值匹配

foreach ($plates as $key => $val) {                        
            $search = array_search('KHX', $plates[$key]);            
            if($search){
                echo $search."\n";
                echo $plates[$key][0];
                break;
            }
        }        
PHP 数组 序列 字符串比较

评论

0赞 bestprogrammerintheworld 5/27/2020
现在我明白了..我认为。。如果我搜索 FNA,我会得到 FNB 的结果吗?如果我搜索 BHI,我会得到 BHG 的结果吗?如果我搜索 CGJ,我会得到 CGJ,因为它已经存在了?
0赞 devjs11 5/27/2020
@bestprogrammerintheworld是的(除了 A 和 I 用于允许的字母),但它非常具有挑战性,因为它必须是最接近可能的序列,因为数组只包含该月的最后注册序列,这意味着我们不能只搜索前两个或前两个字母。序列从 BBB 开始,将在 ZZZ 结束。例如:BBC BBD ...BBZ,然后我们继续 BCB、BCC、BCD 等。
0赞 bestprogrammerintheworld 5/27/2020
所以如果我理解正确的话。如果您搜索 BTG,您会得到结果,因为 BTF 比 BTZ 更接近?

答:

1赞 Raftx24 5/27/2020 #1

你可以用二进制搜索的 O(log n) 来解决它。但在更直接的解决方案中,你可以用 O(n) 求解它。 您可以使用以下算法计算每个单词之间的差异。 ‍‍

<?php

function strToInt($str)
{
    $result = 0;
    for ($i = 0; $i < strlen($str); $i++) {
        $result = $result * 100 + ord($str[$i]);
    }

    return $result;
}

function find($searchStr)
{
    $plates = [
        ['2018','KHF','KHX','KJV','KKN','KLM','KML','KNK','KPD','KPR','KPT','----','----'],
        ['2017','JWN','JXF','JYB','JYT','JZP','KBM','KCH','KCV','KDK','KFB','KFV','KGN'],
        ['2016','JLN','JMF','JMY','JNR','JPK','JRG','JRZ','JSL','JTB','JTR','JVH','JVZ'],
        ['2015','JCK','JCY','JDR','JFG','JFW','JGP','JHJ','JHT','JJH','JJW','JKK','JKZ'],
        ['2014','HVN','HVZ','HWM','HXB','HXN','HYD','HTY','HZB','HZL','HZZ','JBL','JBY'],
        ['2013','HNT','HPC','HPN','HPY','HRK','HRX','HSK','HSR','HSZ','HTK','HTV','HVF'],
        ['2012','HJC','HJM','HKB','HKL','HKX','HLK','HLW','HMD','HML','HMT','HNC','HNK'],
        ['2011','HBP','HCB','HCR','HDC','HDR','HFF','HFT','HGC','HGM','HGX','HHH','HHT'],
        ['2010','GTC','GTS','GVM','GWC','GWV','GXP','GYD','GYM','GYX','GZJ','GZT','HBG'],
        ['2009','GKS','GLC','GLP','GMC','GMN','GNF','GNY','GPJ','GPW','GRM','GSC','GSR'],
        ['2008','FZR','GBN','GCK','GDH','GFC','GFY','GGV','GHG','GHT','GJJ','GJV','GKH'],
        ['2007','FKY','FLV','FNB','FNZ','FRC','FSJ','FTP','FVJ','FWC','FXB','FXY','FYY'],
        ['2006','DVW','DWT','DXZ','DYY','FBC','FCJ','FDP','FFK','FGF','FHD','FJD','FKC'],
        ['2005','DFZ','DGX','DHZ','DKB','DLD','DMJ','DNP','DPK','DRG','DSC','DTB','DVB'],
        ['2004','CRV','CSS','CTT','CVR','CWR','CXT','CYY','CZP','DBJ','DCH','DDG','DFF'],
        ['2003','CDV','CFM','CGJ','CHF','CJC','CKB','CLD','CLV','CMM','CNK','CPF','CRC'],
        ['2002','BSL','BTF','BTZ','BVW','BWT','BXP','BYP','BZF','BZV','CBP','CCH','CDC'],
        ['2001','BFJ','BGF','BHG','BJC','BKB','BLC','BMF','BMW','BNL','BPG','BRB','BRT'],
        ['2000','---','---','---','---','---','---','---','---','BBJ','BCD','BCY','BDR']
    ];

    $minYear = null;
    $minKey = null;
    $minDiff = strToInt('ZZZ');
    $searchInt = strToInt($searchStr);

    for ($i = 0; $i < count($plates); $i++) {
        for ($j = 1; $j < 13; $j++) {
            if(abs($searchInt - strToInt($plates[$i][$j])) < $minDiff) {
                $minDiff = abs($searchInt - strToInt($plates[$i][$j]));
                $minYear = $plates[$i][0];
                $minKey = $plates[$i][$j];
            }
        }
    }

    return [$minYear, $minKey];
}


print_r(find('KHW'));

评论

1赞 devjs11 5/27/2020
谢谢@raftx24,看起来很棒,但不幸的是,我看到它没有正确匹配。如果我搜索 print_r(find('GWN'));它返回错误的 HLW。离GWN最近的是GWV。我想知道为什么会发生这种情况?
0赞 Raftx24 5/27/2020
我想给你一个关于算法的想法,我修复了它。
1赞 bestprogrammerintheworld 5/27/2020 #2

下面的代码绝不是优化的,而是关于如何解决问题的概念。

//Flatten out array (one dimension without years and ----)
$flatten = array();
foreach($plates as $platevalues) {
    foreach($platevalues as $pv) {        
        if ($pv != '---' && $pv != '----' && intval($pv) == 0) {
            //Create a string only if valid letters included in the $letters-array
            //This string is then added to the new array that is flattened out
            $pv2 = '';
            for($i=0;$i<strlen($pv);$i++) {
                $letter = substr($pv,$i,1);
                if (in_array($letter, $letters) !== false) {
                    $pv2 .= $letter;
                }
            }
            $flatten[] = $pv2;
        }
    }
}

//Do the search
$search = 'GWN';
$search_result = '';

//Create a new search string based on first found in flattened
//plates array (first G, then GW, then GWN)
for($i=0;$i<strlen($search);$i++) {
    foreach($flatten as $key=>$f) {
        if (substr($search,0,$i+1) == substr($f,0,$i+1)) {
            $search_result .= substr($search,$i,1);
            break;
        }
    }
}
/*
$search_result is: GW
*/

//Create a new array where all items that begins with GW are included
$result = [];
foreach($flatten as $key=>$item) {
    if (substr($search_result,0,strlen($search_result)) == 
    substr($item,0,strlen($search_result))) {
        $result[] = $item;   
    }
}
/*
$result =

array (size=2)
    0 => string 'GWC' (length=3)
    1 => string 'GWV' (length=3)
*/

//Create an array with total ASCII-value for each item 
//in the $result array above
$result_o = [];
foreach($result as $item) {
    $o = 0;
    for($i=0;$i<strlen($item);$i++) {
        $o += ord(substr($item,$i,1));
    }
   $result_o[]= $o;
}
/* 
$result_o = 

array (size=2)
    0 => int 225
    1 => int 244        
*/


//Get the total ASCII-value for the original search string
$search_o = 0;
for($i=0;$i<strlen($search);$i++) {
    $search_o += ord(substr($search,$i,1));
}
/*
$search_ o = 236
*/


//Find closest value in the $result_o (ASCII) - array compared (225,244)
//to the original $search_o ASCII value above (236)
$closest = 0;
$use_key = 0;
foreach($result_o as $key=>$item) {
    if ($closest == 0 || abs($search_o - $closest) > abs($item - $search_o)) {
        $closest = $item;
        $use_key = $key;
    }
}

/* 
$closest = 244 (it's closer to 236 than 225 is)
$use_key = 1
*/

要获得结果,您需要:

/*
$result =

array (size=2)
    0 => string 'GWC' (length=3)
    1 => string 'GWV' (length=3)
*/

//This should print out GWV
echo 'result=' . $result[$use_key];