AtCoder Beginner Contest 215 C - One More aab aba baa
問題の要約
文字列Sの各文字を並べ替えて作ることが可能な文字列を辞書順に全て列挙したとき,前からK番目にくる文字列を求めよ。
制約
1<=|S|<=8
入力
S K
考え方
1,制約からSの文字数が少ないので,c++の場合next_permutation関数で全ての並べ替えが間にあう。
実際のプログラム
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
string S;
cin >> S;
int K;
cin >> K;
vector<string> Ss;
sort(S.begin(),S.end());
do{
Ss.push_back(S);
}while(next_permutation(S.begin(),S.end()));
sort(Ss.begin(),Ss.end());
cout << Ss[K-1] << endl;
return 0;
}