第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將一個字符串中的通用模式與另一個字符串匹配

將一個字符串中的通用模式與另一個字符串匹配

PHP
阿晨1998 2022-05-27 10:04:23
我有一個以這種格式存儲在數(shù)組中的撲克手列表:$hands = ['AsQdTc9h', 'AsQsTd9d' ...]我還有一個 4 個字符并使用字母的搜索字符串xyzw。例如,搜索字符串可能如下所示:$searchString = 'xyxy';$searchString = 'xyzw';$searchString = 'yzyw';搜索字符串的全部目的是為手中的小寫字符識別所需的模式。因此,如果 searchString 是xyzw,那只是一種說“僅選擇沒有小寫字母相等的手”的方式。如果搜索字符串是xyxy,則表示“僅選擇第一個和第三個小寫字母相等且第二個和第四個小寫字母相等的手”。換句話說,小寫字母必須與搜索字符串的模式匹配。如何在 PHP 中實現(xiàn)它?
查看完整描述

3 回答

?
湖上湖

TA貢獻2003條經(jīng)驗 獲得超2個贊

雖然這看起來像是一個模式匹配任務(wù),但由于字符串的長度非常有限,偽暴力檢查可能是最簡單的。


function pokerMatch(string $hand, string $pattern): bool

{

    $hand = preg_replace('/[^a-z]/', '', $hand);


    for ($i = 0; $i < strlen($hand); $i++) {

        for ($j = $i+1; $j < strlen($hand); $j++) {

            if ($pattern[$i] === $pattern[$j] && $hand[$i] !== $hand[$j]) {

                return false;

            }

            if ($pattern[$i] !== $pattern[$j] && $hand[$i] === $hand[$j]) {

                return false;

            }

        }

    }

    return true;

}

基本上這是做什么的,它遍歷模式字符串,獲取每對字符并檢查:

  • 如果模式位置 i 和 j 中的字母相等,則手串中的字符也必須相等;

  • 如果模式位置 i 和 j 中的字母不同,則手串中的字符也必須不同;

如果其中任何一個不成立 - 模式不匹配

如果在檢查所有對后我們沒有發(fā)現(xiàn)不匹配 - 那么它是匹配的。

用法:

var_dump(pokerMatch('AsQdTc9h', 'xyzw')); // => bool(true)

var_dump(pokerMatch('AsQdTc9h', 'xyzz')); // => bool(false)

var_dump(pokerMatch('AsQsTc9c', 'xxyy')); // => bool(true)

var_dump(pokerMatch('AsQsTc9c', 'zzww')); // => bool(true) (it's agnostic to exact letters)


查看完整回答
反對 回復(fù) 2022-05-27
?
月關(guān)寶盒

TA貢獻1772條經(jīng)驗 獲得超5個贊

$MyString = 'AsQdTc9h';

$MyString = preg_replace('/[^a-z]/', '', $MyString); // Get only the lowercase characters

// $MyString : sdch


$LetterArray = str_split($MyString);

$LetterArray = array_count_values($LetterArray);


$ReplaceList = ['x', 'y', 'z', 'w'];

$i = 0;

foreach ($LetterArray as $Letter => $result) {

    $MyString = str_replace($Letter, $ReplaceList[$i], $MyString);

    $i++;

}

echo $MyString; // expected output : xyzw

我想解釋一下代碼以便您理解它,首先我們使用正則表達式獲取所有小寫字符。然后我們將 4 個字符的單詞轉(zhuǎn)換為一個數(shù)組,然后我們計算有多少個字符是相同的。


然后我們將結(jié)果替換為 xyzw 。


查看完整回答
反對 回復(fù) 2022-05-27
?
小唯快跑啊

TA貢獻1863條經(jīng)驗 獲得超2個贊

您可以通過遍歷每手牌和搜索字符串來解決此問題,記錄哪個花色與哪個搜索字母匹配,并檢查它們是否始終一致:


$hands = ['AsQdTc9h', 'AsQsTd9d', 'AsKh9s9d'];

$searchStrings = ['xyxy', 'xyzw', 'yzyw', 'ppqq'];


function match_hand($hand, $search) {

    $h = preg_replace('/[^a-z]/', '', $hand);

    $matches = array();

    for ($i = 0; $i < strlen($search); $i++) {

        $s = $search[$i];

        // have we seen this search letter before? 

        if (isset($matches[$s])) {

            // does it match the previous value? if not, it's an error

            if ($matches[$s] != $h[$i]) return false;

        }

        else {

            // haven't seen this search letter before, so this hand letter should not be in the matches array yet

            if (in_array($h[$i], $matches)) return false;

        }

        $matches[$s] = $h[$i];

    }

    return true;

}


foreach ($hands as $hand) {

    foreach ($searchStrings as $search) {

        if (match_hand($hand, $search)) {

            echo "hand $hand matches pattern $search\n";

        }

    }

}

輸出:


hand AsQdTc9h matches pattern xyzw

hand AsQsTd9d matches pattern ppqq

hand AsKh9s9d matches pattern yzyw


查看完整回答
反對 回復(fù) 2022-05-27
  • 3 回答
  • 0 關(guān)注
  • 131 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號