Anagram——[枚举全排列]

预备知识:

1.求0—n个数全排列的算法:

 1 void print_permutation(int n,int *A,int cur){
 2     if(cur==n){
 3         for(int i=0;i<cur;i++) cout<<A[i]<<" ";
 4         cout<<endl;
 5     }
 6     else for(int i=1;i<=n;i++){
 7         int ok=1;
 8         for(int j=0;ok&&j<cur;j++)
 9             if(A[j]==i)
10                 ok=0;
11         if(ok){
12             
13             A[cur]=i;
14             print_permutation(n,A,cur+1);
15         }
16     }
17 }

2.另一种求数组A全排列的方法是使用头文件<algorithm>中的next_permutation()函数,使用之前要排序

用法如下:

sort(p,p+n);
do{
   for(int i=0;i<n;i++)cout<<p[i]<<endl;
}while(next_permutation(p,p+n));

下面进入正题:

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

3
aAb
abc
acba

Sample Output

Aab
Aba
aAb
abA
bAa
baA
abc
acb
bac
bca
cab
cba
aabc
aacb
abac
abca
acab
acba
baac
baca
bcaa
caab
caba
cbaa

Hint

An upper case letter goes before the corresponding lower case letter. 
So the right order of letters is 'A'<'a'<'B'<'b'<...<'Z'<'z'.
 
解题思路:
  一道常规的全排列问题,解决思路就是对每个单词进行全排列,然后按设定的优先顺序排序后输出。
注意:
1.“For each word in the input”这句话说明对每一个给定的单词都进行全排列输出,并指明了输出的次序由输入次序决定。
2.注意比较函数的严密性
 
代码参考如下:
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cctype>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <vector>
 7 #include <queue>
 8 #include <time.h>
 9 using namespace std;
10 #define clock__ (double(clock())/CLOCKS_PER_SEC)
11 
12 const int maxs=13;
13 struct word{
14     char p[maxs+3];
15 };
16 
17 bool cmp_letter(const char& a,const char& b){ 
18     if((tolower(a)<tolower(b))||(tolower(a)==tolower(b)&&isupper(a)&&islower(b))) return true;
19     return false;
20 }
21 bool cmp_word(const word &a,const word &b){
22     int len1=strlen(a.p);
23     int len2=strlen(b.p);
24     if(len1<len2) return true;
25     if(len1==len2){
26         for(int i=0;i<len1;i++)
27             if(cmp_letter(a.p[i], b.p[i]))
28                 return true;
29             else if(cmp_letter(b.p[i], a.p[i]))
30                 return false ;
31     }
32     return false;
33 }
34 int n;
35 
36 word p;
37 int main() {
38     scanf("%d",&n);
39     while(n--){
40         vector<word> q;
41         scanf("%s",p.p);
42         int len=strlen(p.p);
43         sort(p.p, p.p+len);
44         do{
45             q.push_back(p);
46         }while(next_permutation(p.p, p.p+len));
47         sort(q.begin(), q.end(),cmp_word);
48         
49         for(int i=0;i<q.size();i++)
50             printf("%s
",q[i].p);
51     }
52     
53     //printf("time : %lf
",clock__);
54     return 0;
55 }
原文地址:https://www.cnblogs.com/Kiraa/p/5281741.html