PHP Group Word algorithm implementation

Source: Internet
Author: User
This time for everyone to bring the PHP Group Word algorithm implementation of the details of the PHP algorithm implementation of the note what, the following is the actual case, together to see.

<?php//Group Word algorithm function diywords ($arr, $m) {  $result = array ();  if ($m ==1) {//Only one word left, return    $arr directly;  }  if ($m = = count ($arr)) {    $result [] = Implode (', $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;} Group Word algorithm $arr=array (' pants ', ' denim ', ' low waist ', ' add fertilizer '), $count =count ($arr); for ($i =1; $i <= $count; $i + +) {  $temp [$i]=diywords ($arr, $i);} Echo ' <pre/> ';p rint_r ($temp);

Operation Result:

Array
(
[1] = = Array
(
[0] = = Pants
[1] = = Cowboy
[2] + = low waist
[3] = + Fertilizer
)
[2] = = Array
(
[0] = = Pants Denim
[1] + = trousers low waist
[2] = + trousers Add Fertilizer
[3] = denim low waist
[4] = + Denim plus Fertilizer
[5] = low waist plus Fertilizer
)
[3] = = Array
(
[0] = pants Denim low waist
[1] = pants denim plus fat
[2] + pants low waist plus fat
[3] = denim low waist plus fertilizer
)
[4] = = Array
(
[0] = pants denim low waist add fertilizer
)
)

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

PHP Implementation Huffman encoding/decoding steps

PHP+MONGODB database Operation steps

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.