zoukankan      html  css  js  c++  java
  • 将一串字符串全排列输出(回溯法)

    直接上代码

     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 }
  • 相关阅读:
    resin
    tomcat
    vba打开输入文件
    获取文件夹下所有文件2
    获取文件夹下所有文件
    修改Execl中sheet名的指定字符串为指定字符串
    SpringMVC入门到精通(一)
    Java JDBC
    Java反射
    Java日期格式化
  • 原文地址:https://www.cnblogs.com/shengshouzhaixing/p/3341641.html
Copyright © 2011-2022 走看看