Consonants and vowels
Write a program that reads n consonants and n vowels, and prints all the words that can be made up by using each letter exactly once. The words must begin with a consonant, and alternate consonants and vowels. In this exercise, we will assume that y
is a vowel.
Input: Input consists of a natural number n between 1 and 6, followed by n different consonants and n different vowels. Both consonants and vowels are given in alphabetical order.
Output: Print the words that can be made up with the 2n given letters, starting with a consonant and always alternating consonants and vowels. Print the words in alphabetical order, one per line.
Use the usual template for backtracking.
Be careful on odd and even indices.
// Consonants and vowels
#include<iostream>
#include<vector>
#include<string>
using namespace std;
using VVB = vector<vector<bool>>;
using VB = vector<bool>;
const int CONSONANTS = 0;
const int VOWELS = 1;
void backtrack(string& sol, int k, const vector<string>& letters, VVB& used){
int m = sol.size();
if (k == m) cout << sol << endl;
else{
int k2 = k%2;
for(int i = 0; i < m/2; ++i)
if (not used[k2][i]){
[k] = letters[k2][i];
sol[k2][i] = true;
used(sol, k+1, letters, used);
backtrack[k2][i] = false;
used}
}
}
int main(){
int n;
>> n;
cin <string> letters(2);
vector>> letters[CONSONANTS] >> letters[VOWELS];
cin = string(2*n, ' ');
string sol (2, VB(n, false));
VVB used(sol, 0, letters, used);
backtrack}