Bagaimanakah saya boleh menjana semua kemungkinan kombinasi saiz tertentu daripada set aksara tertentu menggunakan pendekatan rekursif?

Patricia Arquette
Lepaskan: 2024-11-15 02:46:02
asal
263 orang telah melayarinya

How can I generate all possible combinations of a specific size from a given character set using a recursive approach?

Algorithm for Generating Combinations from a Single Set

The task at hand is to devise an algorithm that can generate all possible combinations of a specified size from a given character set, effectively functioning as a sampling algorithm. Unlike permutation algorithms, this technique allows for the repetition of characters within combinations.

Recursive Approach

To tackle this problem, we employ a recursive function that takes as input the character set, the desired combination size, and an array of intermediate combinations (initialized as the original set for the initial iteration).

  1. Base Case: If the size is 1, the function returns the current set of combinations.
  2. Recursive Step:

    • Create an empty array for the new set of combinations.
    • For each existing combination and character in the set, concatenate them and append them to the new array.
    • Recall the same function with the updated character set (unchanged), reduced size, and new set of combinations as input.

Example Implementation

The following PHP code illustrates the implementation of the recursive algorithm:

function sampling($chars, $size, $combinations = array()) {

    // Base case
    if (empty($combinations)) {
        $combinations = $chars;
    }

    // Size 1 case
    if ($size == 1) {
        return $combinations;
    }

    // Initialize new combinations array
    $new_combinations = array();

    // Generate new combinations by concatenating existing and new characters
    foreach ($combinations as $combination) {
        foreach ($chars as $char) {
            $new_combinations[] = $combination . $char;
        }
    }

    // Recursive call
    return sampling($chars, $size - 1, $new_combinations);

}
Salin selepas log masuk

Example Usage

To demonstrate the functionality, let's consider a set of characters:

$chars = array('a', 'b', 'c');
Salin selepas log masuk

Using the algorithm, we can generate all combinations of size 2:

$output = sampling($chars, 2);
var_dump($output);
Salin selepas log masuk

Output:

array(9) {
  [0]=>
  string(2) "aa"
  [1]=>
  string(2) "ab"
  [2]=>
  string(2) "ac"
  [3]=>
  string(2) "ba"
  [4]=>
  string(2) "bb"
  [5]=>
  string(2) "bc"
  [6]=>
  string(2) "ca"
  [7]=>
  string(2) "cb"
  [8]=>
  string(2) "cc"
}
Salin selepas log masuk

Atas ialah kandungan terperinci Bagaimanakah saya boleh menjana semua kemungkinan kombinasi saiz tertentu daripada set aksara tertentu menggunakan pendekatan rekursif?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan