Problem C
Generating Fast, Sorted Permutation
Input: Standard Input
Output: Standard Output
Generating permutation has always been an important problem in computer science. In this problem you will have to generate the permutation of a given string in ascending order. Remember that your algorithm must be efficient.
Input
The first line of the input contains an integer n, which indicates how many strings to follow. The next n lines contain n strings. Strings will only contain alpha numerals and never contain any space. The maximum length of the string is 10.
Output
For each input string print all the permutations possible in ascending order. Not that the strings should be treated, as case sensitive strings and no permutation should be repeated. A blank line should follow each output set.
Sample Input
3
Sample Output
ab
和上一道题差不多,调用STL
#include<iostream> #include<string> #include<algorithm> using namespace std; bool cmp(char ch1,char ch2) { return ch1<ch2; } int main() { int n; cin>>n; while(n--) { string str; cin>>str; sort(str.begin(),str.end(),cmp); int i,j; cout<<str<<endl; while(next_permutation(str.begin(), str.end())) { cout<<str<<endl; } cout<<endl; } return 0; }