Permutations of words
Write a program that, given n different words s_1, \dots, s_n, prints all the permutations that can be made up with the words.
Input: Input consists of a number n > 0, followed by s_1,\dots, s_n.
Output: Print all the permutations that can be made up with s_1, \dots, s_n.
You can print the solutions to this exercise in any order.
Use the template for backtracking.
A way to do this exercise could be to have a vector<string> words
to store all the words and a vector<int> sol
to store indices of the words in the solution.
// Permutations of words
#include<iostream>
#include<vector>
#include<string>
using namespace std;
void write (const vector<int> & v, const vector<string>& words){
<< '(' << words[v[0]];
cout for (size_t i = 1; i < v.size(); ++i)
<< ',' << words[v[i]];
cout << ')' << endl;
cout }
void perm(vector<int>& sol, int k, vector<bool> used, const vector<string>& words){
int n = sol.size();
if (k == n) write(sol, words);
else{
for (int i = 0; i < n; ++i){
if (not used[i]){
[k] = i;
sol[i] = true;
used(sol, k+1, used, words);
perm[i] = false;
used}
}
}
}
int main(){
int n;
>> n;
cin <string> words(n,"");
vectorfor (int i = 0; i < n; ++i)
>> words[i];
cin <int> sol(n,0);
vector<bool> used(n, false);
vector(sol, 0, used, words);
perm}