Inverse Permutation

closed account (jT59E3v7)
Hi,

I have been given a task to take a permutation of an alphabet string and return the inverse permutation of it.

For example if the character B is in position 3 in the input permutation, then D is in position 1 in the inverse permutation.

01234. . .
ALPHABET: ABCDE. . .
PERMUTATION: ***B*. . .
INVERSE PERMUTATION: *D***. . .

I have written the below code but cannot get my head around how the input string can be compared with the ALPHABET array, and how the inverse can be returned. Any help would be appreciated

#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
#include<limits>
using namespace std;

bool permutationCheck(const string &input)
{
const string ALPHABET("ABCDEFGHIJKLMNOPQRSTUVWXYZ.,' "); //Declaring ALPHABET string to compare against input
if (ALPHABET.length() == input.length()) //comparing the lengths of the ALPHABET string and input to see if it matches
{
if (is_permutation(ALPHABET.begin(), ALPHABET.end(), input.begin())) //Checks if input is permutation of ALPHABET
{

return (true);
}
else
{

return (false);
}
}
else
{

return (false); //Returned if lengths do not match
}
}

int inversePermutation(const string &inverseinput, const string &input)
{
if (permutationCheck(inverseinput) == true) {
cout << "This is a valid permutation" << endl;
char permutation[29] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '.', ',','\'' };

}
else {
return (false);
}

}


int main()
{

string input; //storing the input
cout << "Enter a permutation of the alphabet" << endl;
getline(cin, input);
cout << permutationCheck(input) << endl; //returning the function
system("pause");

cin.ignore(256, '\n');
string inverseinput;
cout << "Enter a Permutation of the alphabet" << endl;
getline(cin, inverseinput);
cout << inversePermutation(inverseinput,input) << endl;
system("pause");
return 0;
}

For example if the character B is in position 3 in the input permutation, then D is in position 1 in the inverse permutation.

I have no idea what you're saying.
Topic archived. No new replies allowed.