Jaewoo Song
Jaewoo Song

Categories

  • Tech

I noticed that there is a function to get permutations of a given sequence.

If I had known that, I could have responded better in Kakao Internship Interview.


The function named next_permutation in <algorithm> library gets the start and end iterator, changes the given array into a next permutation consisting of elements in the range, and returns true.

If all permutations are reached, the input goes back to the original form and the function returns false.


The point is that next_permutation function does not calculate the order of permutations, so it returns false after making $n$th permutation if there are $1$st to $n$th permutations in ascending order.

So if we want to get all of the permutations, the original form of the array should be sorted in ascending order at the beginning.

vector<char> ges = {'h', 'p', 's'};

do {
    poss.push_back(ges);
} while(next_permutation(ges.begin(), ges.end()));


With the above codes, I could solve this problem easily.


https://www.acmicpc.net/problem/14456