PHP permutation recursive and permutation combination instance code detailed

Source: Internet
Author: User
1. Arranging recursion

If p represents the full arrangement of n elements, and Pi means that n elements do not contain the full permutation of element I, (i) pi indicates the arrangement of the prefix I in front of the permutation pi, then the full arrangement of n elements is recursively defined as:
① if n=1, then the permutation p has only one element i;
② if n>1, then the entire arrangement p is composed of the permutation (i) pi;
By definition, you can see that if you have generated the permutation pi for (k-1) elements, then the arrangement of the K elements can be generated by adding element I to each pi.

Code:

function rank ($base, $temp =null) {    $len = strlen ($base);    if ($len <= 1)    {        echo $temp. $base. ' <br/> ';    }    else    {for        ($i =0; $i < $len; + + $i)        {            rank (substr ($base, 0, $i). substr ($base, $i +1, $len-$i-1), $temp. $base [$i]);}}    Rank (' 123 ');

However, after several tests of the results of the operation, it was found that there was a problem: if the same element exists, then the whole permutation is repeated.
For example, there are only three cases of the full arrangement of ' 122 ': ' 122 ', ' 212 ', ' 221 ';
Slightly modified, add a judge to repeat the flag, resolved the problem (code below):

function Fsrank ($base, $temp =null) {    static $ret = Array ();    $len = strlen ($base);    if ($len <= 1)    {        //echo $temp. $base. ' <br/> ';        $ret [] = $temp. $base;    }    else    {for        ($i =0; $i < $len; + + $i)        {            $had _flag = false;            for ($j =0; $j < $i; + + $j)            {                if ($base [$i] = = $base [$j])                {                    $had _flag = true;                    break;                }            }            if ($had _flag)            {                continue;            }            Fsrank (substr ($base, 0, $i). substr ($base, $i +1, $len-$i-1), $temp. $base [$i]);        }    return $ret;} print ' <pre> ';p rint_r (Fsrank (' 122 '));p rint ' </pre> ';

2. Permutation combination Instances

<?php/** * Math problem to solve: Calculate C (a,1) * C (b, 1) * ... * combination of C (n, 1), where C (n, 1) represents an arbitrary element from n elements * * Examples of practical problems to solve: A grade has m classes, the number of each class Different, now to draw from each class A person to form a group, * by the group to represent the grade to participate in the school's activities, please give all possible combinations *//* ################################### start Calculate ################################### *//** * Array Description: The array is a two-dimensional array, the first-dimensional index represents the class number, the second-dimensional index represents the student number */$CombinList =                    Array (1 = = Array ("Student10", "Student11"), 2 = = Array ("Student20", "Student21", "Student22"), 3 = = Array ("Student30"), 4 = = Array ("STUDENT40", "Student41", "Student42", "Stud Ent43 ") */* calculate C (a,1) * C (b, 1) * ... * C (n, 1) value */$CombineCount = 1;foreach ($CombinList as $Key + $Value) {$Combin Ecount *= count ($Value);}     $RepeatTime = $CombineCount, foreach ($CombinList as $ClassNo + $StudentList) {//$StudentList the maximum number of repetitions that occur vertically after splitting a component into a combination    $RepeatTime = $RepeatTime/count ($StudentList);    $StartPosition = 1; Start looping for students in each class foreach ($StudentList as $Student) {$TempStartPosition = $StartPosition;        $SpaceCount = $CombineCount/count ($StudentList)/$RepeatTime;               for ($J = 1; $J <= $SpaceCount; $J + +) {for ($I = 0; $I < $RepeatTime; $I + +) {            $Result [$TempStartPosition + $I] [$ClassNo] = $Student;        } $TempStartPosition + = $RepeatTime * COUNT ($StudentList);    } $StartPosition + = $RepeatTime; }}/* print result */echo "<pre>";p Rint_r ($Result);? >

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.