Using C++, write a program that satisfies this prompt about permutations: Using
ID: 667593 • Letter: U
Question
Using C++, write a program that satisfies this prompt about permutations:
Using C++, write a program that satisfies this prompt about permutations: A permutation of a string is the set of all possible ways to combine its characters. E.g.. the permutation of ^''abc^'' is {^''abc^'', ^''acb^'', ^''bac^'', ^''bca^'', ^''cab^'', ^''cba^'' . The size of this set is the factorial of the initial string size. Given a string S (with up to 20 characters, all lowercase letters) and a integer N (0 less than equal to NExplanation / Answer
#include <iostream>
#include <algorithm>
#include <fstream>
using namespace std;
void permute(string a, int l, int r, int need, int *count){
if(l == r){
if(*count == need){
cout << a << " ";
}
(*count)++;
}
else{
for(int i = l; i <= r; i++){
char temp = a[l];
a[l] = a[i];
a[i] = temp;
permute(a, l + 1, r, need, count);
temp = a[l];
a[l] = a[i];
a[i] = temp;
}
}
}
int main(){
ifstream inFile("input.txt");
int n;
inFile >> n;
while(n--){
int count = 0;
string str;
int need;
inFile >> str;
inFile >> need;
sort(str.begin(), str.end());
permute(str, 0, str.size() - 1, need, &count);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.