Home > Daily Programming > PHP Knowledge > How to implement full permutation and combination of strings in PHP? (Pictures + Videos)

How to implement full permutation and combination of strings in PHP? (Pictures + Videos)

藏色散人
Release: 2018-10-11 13:46:47
Original
7401 people have browsed it

This article mainly introduces to you PHP string full arrangement algorithmHow to implement it specifically.

I believe everyone is familiar with the concept of total permutation, because it is an essential course knowledge point in high school or college mathematics texts. The total arrangement is to randomly select m (m≤n) elements from n different elements and arrange them in a certain order. It is called an arrangement in which m elements are taken from n different elements. When m=n, all arrangements are The situation is called full arrangement.

So in our PHP learning process, we will also encounter problems about the full arrangement of strings.

Below we will introduce the PHP string full arrangement algorithm in detail through specific code examples.

<?php
$str = &#39;abc&#39;;
// 字符串转换为数组
$a = str_split($str);
// 调用perm函数
perm($a, 0,count($a) - 1);
/**
 * 定义perm函数
 * @param $ar // 排列的字符串
 * @param $k // 初始值
 * @param $m // 最大值
 */
function perm(&$ar, $k, $m)
{
    // 初始值是否等于最大值
    if ($k == $m) {
        // 将数组转换为字符串
        echo join(&#39;&#39;, $ar), PHP_EOL;
    } else {
        // 循环调用函数
        for ($i = $k; $i <= $m; $i++) {
            // 调用swap函数
            swap($ar[$k], $ar[$i]);
            // 递归调用自己
            perm($ar, $k + 1, $m);
            // 再次调用swap函数
            swap($ar[$k], $ar[$i]);
        }
    }
}

function swap(&$a, &$b)
{
    $c = $a;
    $a = $b;
    $b = $c;
}
Copy after login

Here we mainly define two methods, perm and swap function, and annotate the meaning of each step of operation in detail in the code, which is helpful for your reference and understanding.

So in the above code, we involve the recursive algorithm. Friends who are unclear about this knowledge point can refer to this article [How to implement recursive sorting in PHP? ].

Finally accessed through the browser, The strings are all arrangedThe results are as follows:

How to implement full permutation and combination of strings in PHP? (Pictures + Videos)

This article is about PHP realizes the full permutation and combination of stringsThe algorithm introduction has a certain reference value. I hope it will be helpful to friends in need!

If you want to know more about PHP, you can follow the PHP Chinese website PHP Video Tutorial, everyone is welcome to refer to and learn!

The above is the detailed content of How to implement full permutation and combination of strings in PHP? (Pictures + Videos). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template