2020年2月14日 星期五

10098 - Generating Fast, Sorted Permutation

這題zerojudge上測資有問題,丟uva就過了,傻眼orz
其實對這種字串排序還是不怎麼熟,是把工具套一套解出來的。

步驟:
1.對字串進行排序
2.next_permutation

程式碼:
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int n;
    cin>>n;
    getchar();
    while(n--)
    {
        string s;
        getline(cin,s);
        int len=s.size();
        sort(s.begin(),s.end());
        do{
            cout<<s<<endl;
        }while(next_permutation(s.begin(),s.end()));
        cout<<endl;
    }
    return 0;
}

參考:https://stackoverflow.com/questions/17006808/find-all-permutations-of-a-string-in-c

next_permutation是字典序較大的放右邊,而與之相反的則是prev_permutation。

相關題目:d703: SOS ~~~

沒有留言:

張貼留言