How to get inverse permutation of string based on array position

I have two separate codes that work separate, but they don't really accomplish what I want. I have a code that spits out the output inverse permutation of an array and I have a code that takes a string and inverts it to the nth number.. However, what I really want is a single code that allows for the input of a string and then provides the inverse of the string based on the location of the array.

Sort of like putting in the string
perm("abc", "012") == "abc"
perm("abc", "210") == "cba"
perm("abc", "201") == "cab"
I'm confused on how to combine the codes to get them to work. Am I on the right track?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  #include <bits/stdc++.h> 
using namespace std; 

void nPermute(string str, long int n) 
{ 
  
    sort(str.begin(), str.end()); 
  
    
    long int i = 1; 
    do { 
      
        if (i == n) 
            break; 
  
        i++; 
    } while (next_permutation(str.begin(), str.end())); 
  
   
    cout << str; 
} 
  

int main() 
{ 
    string str = "INVERSE"; 
    long int n = 100; 
    nPermute(str, n); 
printf("\n"); 
    return 0; 
} 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
using namespace std;

void inverse(int arr[], int size) {
  int arr2[size];
  for (int i = 0; i < size; i++)
    arr2[arr[i] - 1] = i + 1;
  for (int i = 0; i < size; i++)
    cout << arr2[i] << " ";
}

int main() {
  int arr[] = {9, 5, 6, 10, 1, 2, 3, 4, 7, 8};

  int size = sizeof(arr) / sizeof(arr[0]);
  inverse(arr, size);
printf("\n"); 

}
Last edited on
Sort of like putting in the string
This is neither permutation nor inverse. It is actually just reorder.

How does permutation come into play? Inverse permutation doesn't make sense since when you have all permutations how could it possibly inversed?
Sort of like putting in the string
perm("abc", "012") == "abc"
perm("abc", "210") == "cba"
perm("abc", "201") == "cab"

Here is code that does that. Like others, I don't know what you mean by "inverse permutation."
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
using namespace std;

string perm(const string &str, const string &pos)
{
    string result;
    for (unsigned i=0; i<pos.size(); ++i) {
	unsigned idx = pos[i] - '0';
	result += str[idx];
    }
    return result;
}

int
main()
{
    cout << perm("abc", "012") << '\n';
    cout << perm("abc", "210") << '\n';
    cout << perm("abc", "201") << '\n';
}

That actually helps a lot. I was told it was supposed to be an inverse, but a reorder makes a lot more sense. I made a code for the reorder that works solely in arrays. what int would I use to make it take arrays and strings?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream> 
  
using namespace std; 
  
// Function to reorder elements of arr[] according 
// to index[] 
void reorder(int arr[], int index[], int n) 
{ 
    int temp[n]; 
  

    for (int i=0; i<n; i++) 
        temp[index[i]] = arr[i]; 
  
    for (int i=0; i<n; i++) 
    {  
       arr[i]   = temp[i]; 
       index[i] = i; 
    } 
} 
  

int main() 
{ 
    int arr[] = {3, 2, 1, 5, 6, 4}; 
 int arb[] = {5, 6, 1, 3, 2, 4}; 
    int index[] = {3, 4, 2, 0, 1, 5}; 

    int n = sizeof(arr)/sizeof(arr[0]);
  cout << "\nSequence array is: \n"; 
      for (int i=0; i<n; i++) 
        std::cout << index[i] << ' ';
  cout << "\nOriginal array is: \n"; 
   for (int i=0; i<n; i++) 
        cout << arr[i] << " "; 
printf("\n");

    reorder(arr, index, n); 
   reorder(arb, index, n);
 
    cout << "Reordered array is: \n"; 
    for (int i=0; i<n; i++) 
        cout << arr[i] << " "; 

printf("\n");
    
printf("\n");
 

    return 0; 
}
Last edited on
Are you sure line 13 is right? That doesn't do what your example in the original post shows. Shouldn't it be tmp[i] = arr[index[i]];?
Topic archived. No new replies allowed.