将一串字符串全排列输出(回溯法)

直接上代码

 1 //Author      :Freetion
 2 //E-mail      :Freetion@live.com
 3 //file        :
 4 
 5 #include <stdio.h>
 6 #include <string.h>
 7 char str[1000], stack[1000];;
 8 char ch[256];
 9 int count[256];
10 int char_num, str_len;
11 
12 void deal_str()
13 {
14     int s[256];
15     memset(s, 0, sizeof(s));
16     memset(count, 0, sizeof(count));
17     str_len = strlen(str);
18     for (int i = 0; i < str_len; i ++)
19         s[str[i]] ++;
20     char_num = 0;
21     for (int i = 0; i < 256; i ++)
22     {
23         if (s[i])
24         {
25             count[char_num] = s[i];
26             ch[char_num ++] = i;
27         }
28     }
29     return;
30 }
31 
32 void found(int depth)
33 {
34     
35     if (depth == str_len)
36     {
37         for (int i = 0; i < depth; i ++)
38             putchar(ch[stack[i]]);
39         puts("");
40     }
41     else
42     {
43         for (int i = 0; i < char_num; i ++)
44         {
45             if (count[i])
46             {
47                 stack[depth] = i;
48                 count[i] --;
49                 found(depth +1);
50                 count[i] ++;
51             }
52         }
53     }
54     return;
55 }
56 
57 int main()
58 {
59     while (gets(str))
60     {
61         deal_str();
62         found(0);
63     }
64     return 0;
65 }

 

posted on 2013-09-26 21:52  圣手摘星  阅读(508)  评论(0编辑  收藏  举报

导航