字符全排列

来源:互联网 发布:孔玮怎么样知乎 编辑:程序博客网 时间:2024/05/17 04:37

Description

You are to write a program that has to generate all possible words from a given set of letters. 
Example: Given the word "abc", your program should - by exploring all different combination of the three letters - output the words "abc", "acb", "bac", "bca", "cab" and "cba". 
In the word taken from the input file, some letters may appear more than once. For a given word, your program should not produce the same word more than once, and the words should be output in alphabetically ascending order. 

Input

The input consists of several words. The first line contains a number giving the number of words to follow. Each following line contains one word. A word consists of uppercase or lowercase letters from A to Z. Uppercase and lowercase letters are to be considered different. The length of each word is less than 13.

Output

For each word in the input, the output should contain all different words that can be generated with the letters of the given word. The words generated from the same input word should be output in alphabetically ascending order. An upper case letter goes before the corresponding lower case letter.

Sample Input

3aAbabcacba

Sample Output

AabAbaaAbabAbAabaAabcacbbacbcacabcbaaabcaacbabacabcaacabacbabaacbacabcaacaabcabacbaa
/**这个题由于一个小错误被我提交了n次,终于被我改对了,可是感觉心好累啊!!一开始问学长,学长说我思路有问题,其实就是在next_permutation()里面少了一个cmp而已,结果害的我。。。。。不说了*/#include<cstring>#include<string>#include<cstdio>#include<algorithm>using namespace std;bool cmp(char x, char y){    double aa = (double)x;    double bb = (double)y;    if(aa > 90)        aa = aa - 31.5;    if(bb > 90)        bb = bb - 31.5;    return aa < bb;}int main(){    int N;    scanf("%d",&N);    while(N--)    {        char a[20];        scanf("%s",a);        sort(a,a+strlen(a),cmp);        do        {            printf("%s\n",a);        }        while(next_permutation(a,a+strlen(a),cmp));    }    return 0;}

0 0