題目連結:AtCoder Beginner Contest 215: C – One More aab aba baa
題目敘述 :
輸出 S 中字典順序第 K 大的排列.
範例輸入 1 :
aab 2
範例輸出 1 :
aba
範例輸入 2 :
ydxwacbz 40320
範例輸出 2 :
zyxwdcba
題解 :
用內建函式next_permutation()可以生成字典序大一點的排列。
#include<bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);cin.tie(0);
string str;
int n;
cin>>str>>n;
// 因為沒有說輸入的會是字典序最小的,因此要先排序
sort(str.begin(),str.end());
// 會是n-1是因為還沒進行前就是最小(第k=1小)
for(int i=0;i<n-1;++i){
next_permutation(str.begin(),str.end());
}
cout<<str;
}