您的位置:首页 > 其它

UVa10098 Generating Fast, Sorted Permutation

2013-07-18 16:17 309 查看
Problem C

[b]Generating Fast, Sorted Permutation
[/b]

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

ab

abc

bca

 

Sample Output

ab

ba

abc

acb

bac

bca

cab

cba

abc

acb

bac

bca

cab

cba

Shahriar Manzoor

这题也确实没有什么好说的,简单的全排列题目,next_permutation()真的挺好用的。

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

const int N = 20;
char s
;
int main(){
int n;
cin >> n;
while (n--){
memset(s,0,sizeof(s));
cin >> s;
int len = strlen(s);
sort(s,s+len);
cout << s << endl;
while (next_permutation(s,s+len)){
cout << s << endl;
}
cout << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uva 算法