All permutations of length k from n characters with repetition in CPP

后端 未结 4 1427
Happy的楠姐
Happy的楠姐 2020-12-15 14:43

I would like to know if there is already an implementation in CPP to find all permutations of n characters of length k(1,2,3,4 etc) with repetitions. I hope there is but i c

相关标签:
4条回答
  • 2020-12-15 15:02

    you can use std::next_permutation() as πάντα ῥεῖ said, but since you want to define the length and the char with repetitions, you can do something easy to realize it, as:

        std::string s = "aabbccdd";
        std::set<std::string> string_set;
        std::sort(s.begin(), s.end());
        do {
            string_set.insert(s.substr(0, 2));
        } while(std::next_permutation(s.begin(), s.end()));
        for(auto i = string_set.begin(); i != string_set.end(); ++i)
            std::cout << *i << std::endl;
    
    0 讨论(0)
  • 2020-12-15 15:09

    Simple recursive solution which will work for you for sure.

    Let me first re-write your specification: Print all permutations with repetition of characters

    Given a string of length n, print all permutation of the given string. Repetition of characters is allowed

    For a given string of size n, there will be n^k possible strings of length "length". The idea is to start from an empty output string (we call it prefix in following code). One by one add all characters to prefix. For every character added, print all possible strings with current prefix by recursively calling for "length" equals to "length"-1.

    #include <string>
    #include <iostream>
    
    
    void print_str(const char*,std::string,const int, const int);
    
    int main()
    
    {
    
        int lenght = 2;
    
        char str[] = {'A', 'B', 'C', 'D'};
    
    
    
        int n = sizeof str;
    
        print_str(str, "", n, lenght);  //Note: this function works on all cases and not just the case above
    
        return 0;
    
    }
    
    // The main recursive method to print all possible strings of length "length"
        void print_str(const char str[],std::string prefix,const int n, const int lenght)
    
        {
    
            if (lenght == 1)
    
                {
    
                    for (int j = 0; j < n; j++)
    
                    std::cout << prefix + str[j] << std::endl;
    
                }//Base case: lenght = 1, print the string "lenght" times + the remaining letter
    
            else
    
                {
    
    
                   // One by one add all characters from "str" and recursively call for "lenght" equals to "lenght"-1
                    for (int i = 0; i < n; i++)
    
                    // Next character of input added
                    print_str(str, prefix + str[i], n, lenght - 1);
                    // "lenght" is decreased, because we have added a new character
    
                }
    
        }
    

    Here is the execution of the code above:

    References:

    http://www.geeksforgeeks.org/print-all-permutations-with-repetition-of-characters/

    http://www.geeksforgeeks.org/print-all-combinations-of-given-length/

    Update: this update is writen answring the following spec.

    I need one more help!! as i am new to CPP programming. Suppose if length = 3 how can i make it to get all permutations starting from length = 1 to length = 3 together in an array. Means to get all the permutations of length =1, length =2 and length = 3 together stored in an array

    #include <string>
    #include <iostream>
    #include <vector>
    
    void print_str(const char*,std::string,const int, const int);
    std::vector<std::string> permutations ;  // the vector permutations which will hold all the permutations, 
                                           //if you want you can use it for later use or you can use the array below which is nothing than a copy of this vector.
    
    int NumberOfPermutations = 0; // this variable holds the number of permutations 
    
    
    
    int main()
    
    {
    
        int lenght = 3;
    
        char str[] = {'A', 'B', 'C', 'D'};
    
        int n = sizeof str;
    //here we loop through all the possible lenghts 1, 2 and 3
        for (int k = 1; k <= lenght; k++)
    
                   {
    
                       print_str(str, "", n, k);  //Note: this function works on all cases and not just the case above
                    }
    
    
        std::string* permut_array =  new std::string[NumberOfPermutations]; // the array that we will use to store the permutations in
    
        std::copy(permutations.begin(), permutations.end(), permut_array); // here we copy the vector into the array 
    
        //if you want you can use your array to print the permutation as folow
    
        for (int k = 0; k < NumberOfPermutations; k++)
    
                   {
    
                       std::cout << permut_array[k] << std::endl;
                    }
    
        return 0;
    
    }
    
    // The main recursive method to print all possible strings of length "length"
        void print_str(const char str[],std::string prefix,const int n, const int lenght)
    
        {
    
            if (lenght == 1)
    
                {
    
                    for (int j = 0; j < n; j++)
    
                   {
                       // i commented this ligne so that if you want to use your array to print your permutations you will not get a screnn with permutations printed 2 times
                       //std::cout << prefix + str[j] << std::endl; 
                       permutations.push_back(prefix + str[j]); // the vector that we will use to store the permutations in
                    }
    
                }//Base case: lenght = 1, print the string "lenght" times + the remaining letter
    
            else
    
                {
    
    
                   // One by one add all characters from "str" and recursively call for "lenght" equals to "lenght"-1
                    for (int i = 0; i < n; i++)
    
                    // Next character of input added
                     print_str(str, prefix + str[i], n, lenght - 1);
                    // "lenght" is decreased, because we have added a new character
    
                }
            NumberOfPermutations = permutations.size();
        }
    
    0 讨论(0)
  • 2020-12-15 15:18

    This just isn't a permutation, which probably explains why you can't find an answer.

    What you are actually asking is how to print the numbers 0..k-1 in base n, using digits A,B,C,D. I'll rewrite your example with familiar digits 0,1,2,3 :

    00
    01
    02
    03
    10
    11
    12
    13
    ..
    33
    

    There's no standard C++ method for this, but now that you know what it's called there's plenty of code on the web. Or just write it yourself. Hint: the last digit of i has value i % n.

    0 讨论(0)
  • 2020-12-15 15:19

    This solution works out for all standard container and also static arrays. I think this can be used also for classes and structures

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <numeric>
    #include <list>
    #include <iterator>
    
    template<typename InputIt, typename T>
    bool nextPermutationWithRepetition(InputIt begin, InputIt end, T from_value, T to_value) {
        auto it = std::find_if_not(std::make_reverse_iterator(end),
                                   std::make_reverse_iterator(begin),
                                   [&to_value](auto current) { return to_value == current; });
    
        if (it == std::make_reverse_iterator(begin))
            return false;
    
        auto bound_element_iterator = std::prev(it.base());
    
        (*bound_element_iterator)++;
        std::fill(std::next(bound_element_iterator), end, from_value);
    
        return true;
    }
    
    int main() {
        std::list<int> vec(3, 0);
    
        do {
            std::copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout, " "));
            std::cout << std::endl;
        } while (nextPermutationWithRepetition(vec.begin(), vec.end(), 0, 2));
    
        return  0;
    }
    
    0 讨论(0)
提交回复
热议问题