Home  >  Article  >  Backend Development  >  Detailed explanation of PHP word grouping algorithm implementation

Detailed explanation of PHP word grouping algorithm implementation

php中世界最好的语言
php中世界最好的语言Original
2018-05-17 10:12:481640browse

This time I will bring you a detailed explanation of the implementation of the PHP word grouping algorithm. What are the precautions for the implementation of the PHP word grouping algorithm. The following is a practical case, let's take a look.

<?php
//组词算法
function diyWords($arr,$m){
  $result = array();
  if ($m ==1){//只剩一个词时直接返回
    return $arr;
  }
  if ($m == count($arr)){
    $result[] = implode(&#39;&#39; , $arr);
    return $result;
  }
  $temp_firstelement = $arr[0];
  unset($arr[0]);
  $arr = array_values($arr);
  $temp_list1 = diyWords($arr, ($m-1));
  foreach ($temp_list1 as $s){
    $s = $temp_firstelement.$s;
    $result[] = $s;
  }
  $temp_list2 = diyWords($arr, $m);
  foreach ($temp_list2 as $s){
    $result[] = $s;
  }
  return $result;
}
//组词算法
$arr=array(&#39;裤子&#39;,&#39;牛仔&#39;,&#39;低腰&#39;,&#39;加肥&#39;);
$count=count($arr);
for($i=1;$i<=$count;$i++){
  $temp[$i]=diyWords($arr,$i);
}
echo &#39;<pre/>';print_r($temp);

Run result:

Array
(
[1] => Array
(
) [0] => Pants
                                                                                                                       # #                                                                                                                                                       # ] => Denim and fattening
                                                                                                                                                                                                     . Waist
                                                                                                                                                                        . 4] => Array
        (
            [0] => 裤子牛仔低腰加肥
        )
)


相信看了本文案例你已经掌握For more exciting methods, please pay attention to other related articles on the php Chinese website!

Recommended reading:


Detailed explanation of the steps to implement Huffman encoding/decoding in PHP




Detailed explanation of the steps for PHP mongoDB database operation


The above is the detailed content of Detailed explanation of PHP word grouping algorithm implementation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn