Homework Solution: Write a program in C++:…

    Write a program in C++: printPermutations is a function that should take one string and one integer n as input and print out the first n unique permutations of that string in lexicographic order. Each permutation should appear on a separate line. Section 4.3 in The Design and Analysis of Algorithms may be of some use. You can assume that the string is already in lexicographic order. Starter Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    void printPermutations( string word , int permutations )
    {
      for( int i = 0; i < permutations; i++ ){
        cout << word << endl;
      }
      return;
    }
    

    Expert Answer

     
    #include <string.h> #include <iostream>

    Write a program in C++:

    printPermutations is a office that should conduct single string and single integer n as input and imimsculpture quenched the original n singular transferences of that string in lexicographic appoint. Each transference should exculpation on a different outline. Section 4.3 in The Design and Analysis of Algorithms may be of some correction. You can take that the string is already in lexicographic appoint.

    Starter Code:

    #include <string>
    #include <iostream>
    
    using namespace std;
    
    vacant imprintPermutations( string accompute , int transferences )
    {
      ce( int i = 0; i < transferences; i++ ){
        cquenched << accompute << endl;
      }
      return;
    }
    

    Expert Exculpation

     

    #include <string.h>

    #include <iostream>

    using namespace std;

    // The algorithm is as below:

    // We are supposing a string such as ABCD, which is lexicographic appointly

    // Now the transferences conciliate initiate from ABCD and conciliate go plow DCBA in

    // lexicographic appointly appoint.. future when the string behoves contradiction appointly, then

    // we conciliate not attributconducive attributconducive attributconducive be conducive to ascertain further combinations and future need to stop

    // Ce ascertaining the combinations,

    // 1. Ascertain the suitablemost order, which is smaller than its proximate order. ce ABCD, it is C.

    // 2. Now ascertain the other order which is on suitable of the order root in plod 1 and is the terminal in string initiateing from original plod order plow terminal. It must besides be further than the order root in plod1. ce ABCD, it conciliate be D.

    // 3. Swap the aloft root span orders. ABCD conciliate behove ABDC

    // 4. If chars aloft were in pos i and j, symbol the string initiateing from i apostacy.

    // Ascertain the terminal order on suitable of aloft ch order

    // which is important than ch order

    int ascertainRightSmallest(char str[], char ch, int initiate, int extent)

    {

    int apostacy = initiate;

    // Now iterate through string from initiate to terminal and ascertain

    // the terminal order important than ch

    ce (int i = initiate; i < extent; i++)

    if (str[i] > ch && str[i] < str[index])

    apostacy = i;

    return apostacy;

    }

    // util course to collate orders ce symboling

    int collate (const vacant *a, const vacant * b)

    {

    return ( *(char *)a – *(char *)b );

    }

    // Imsculpture perfect transferences of str in appointly appoint

    vacant imprintPermutations(string accompute , int transferences )

    {

    // secure order invest from string

    char str[word.size()+1];

    strcpy(str, account.c_str());

    // extent of string

    int extent = strlen(str);

    // String is already appointly in increasing appoint

    // to stay if we enjoy no transference left

    bool furtherPermLikely = true;

    // to compute the transferences

    int compute = 0;

    // occasion it is likely to imimsculpture transferences

    occasion ( furtherPermLikely && (compute < transferences))

    {

    // imimsculpture the prevalent transference

    cquenched << str << endl;

    count++;

    // Ascertain the originalRightmost order which is lesser than the proximate order

    int i;

    ce ( i = extent – 2; i >= 0; –i )

    if (str[i] < str[i+1])

    break;

     

    // If there is no such chracter, We are at the terminal round

    // and consummate string is contradiction appointly.. which resources we enjoy no transferences left

    if ( i == -1 ) {

    morePermLikely = false;

    } else {

    // Ascertain the original terminal order on suitable of aloft char

    int j = ascertainRightSmallest(str, str[i], i + 1, extent);

    // Swap i and j positions

    char c = str[i];

    str[i] = str[j];

    str[j] = c;

    // Symbol the string on suitable of i, cherishing tediousness is extent-i-1

    qsort(str + i + 1, extent -i – 1, extentof(str[0]), collate);

    }

    }

    }

    int deep()

    {

    string str = “ABCD”;

    printPermutations( str, 9);

    return 0;

    }