用C打印字符串的所有排列

时间:2013-06-07 17:22:02

标签: c string algorithm permutation

我正在学习回溯和递归,我坚持使用算法来打印字符串的所有排列。我使用bell algorithm进行排列解决了但是我无法理解递归方法。我搜索了网络,发现了这段代码:

void permute(char *a, int i, int n) 
{
   int j; 
   if (i == n)
     printf("%s\n", a);
   else
   {
        for (j = i; j <= n; j++)
       {
          swap((a+i), (a+j));
          permute(a, i+1, n);
          swap((a+i), (a+j)); 
       }
   }
} 

这个算法基本上如何工作我无法理解?我甚至尝试过干跑!

如何应用回溯?

它是否比贝尔算法更有效地计算排列?

9 个答案:

答案 0 :(得分:24)

该算法基本上适用于此逻辑:

字符串X的所有排列与X中每个可能字符的所有排列相同,并且与字符串X中没有该字母的所有排列相结合。

也就是说,“abcd”的所有排列都是

  • “a”与“bcd”的所有排列相结合
  • “b”与“acd”的所有排列相结合
  • “c”与“坏”的所有排列相结合
  • “d”与“bca”的所有排列相结合

该算法特别是不对子串执行递归,而是在输入字符串上执行递归,不使用额外的内存来分配子串。 “回溯”撤消对字符串的更改,使其保持原始状态。

答案 1 :(得分:11)

代码有2个问题,两者都与n相关,字符串的假定长度。代码for (j = i; j <= n; j++) { swap((a+i), (a+j)); ...交换字符串的空字符'\0'并提供代码截断结果。检查原来的(i == n) (i == (n-1))

通过调用swap()两次有效撤消其原始交换来应用回溯。

贝尔算法的复杂度顺序相同。

#include <stdio.h>

void swap(char *a, char *b) { char t = *a; *a = *b; *b = t; }

void permute(char *a, int i, int n) {
   // If we are at the last letter, print it
   if (i == (n-1)) printf("%s\n", a);
   else {
     // Show all the permutations with the first i-1 letters fixed and 
     // swapping the i'th letter for each of the remaining ones.
     for (int j = i; j < n; j++) {
       swap((a+i), (a+j));
       permute(a, i+1, n);
       swap((a+i), (a+j));
     }
  }
}

char s[100];
strcpy(s, "ABCD");
permute(s, 0, strlen(s));

答案 2 :(得分:9)

您找到的代码是正确的!该算法将字符串中的当前字符与所有后续字符交换,并递归调用该函数。用文字难以解释。下图可能对您有所帮助。

在第二次交换中正在进行反向跟踪以反转第一次交换的效果,即我们将返回到原始字符串,现在将使用当前字符交换数组中的下一个字符。算法的时间复杂性。因为循环运行n次并且置换函数被称为n,所以是O(n * n!)!倍。 (对于第一次迭代,它被称为n次;对于第二次迭代(n-1)次,依此类推)。

Recursion tree for permutations of string "ABC"

来源http://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/

答案 3 :(得分:5)

递归确实简化了它:

public static void permutation(String str) 
{ 
    permutation("", str); 
}

private static void permutation(String prefix, String str) 
{
    int n = str.length();
    if (n == 0) {
        System.out.println(prefix);
    } else {
        for (int i = 0; i < n; i++)
            permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n));
    }
}

答案 4 :(得分:1)

伪代码:

String permute(String a[])
{
  if (a[].length == 1)
     return a[];
  for (i = 0, i < a[].length(); i++)
    append(a[i], permute(a[].remove(i)));
}

答案 5 :(得分:1)

I create more specific but not efficient Program for permutation for general string.
It's work nice way.
//ubuntu 13.10 and g++ compiler but it's works on any platform and OS
//All Permutation of general string.

#include<iostream>
#include<stdio.h>
#include<string>
#include<string.h>
using namespace std;
int len;
string str;

void permutation(int cnum)
{
    int mid;
    int flag=1;
    int giga=0;
    int dead=0;
    int array[50];
    for(int i=0;i<len-1;i++)
    {
        array[50]='\0';
        dead=0;
        for(int j=cnum;j<len+cnum;j++)
        {
            mid=j%len;
            if(mid==cnum && flag==1)
            {
                cout<<str[mid];
                array[dead]=mid;
                dead++;
                flag=0;
            }
                else
            {
                giga=(i+j)%len;
                for(int k=0;k<dead;k++)                 
                {
                    if((array[k]==giga) && flag==0)
                    {
                    giga=(giga+1)%len;
                    }
                }
                cout<<str[giga];
                array[dead]=giga;
                dead++;

            }
        }
        cout<<endl;
        flag=1; 
    }
}
int main()
{
    cout<<"Enter the string :: ";
    getline(cin,str);
    len=str.length();
    cout<<"String length = "<<len<<endl;
    cout<<"Total permutation = "<<len*(len-1)<<endl;
    for(int j=0;j<len;j++)
    {
        permutation(j);
    }
return 0;
}

答案 6 :(得分:0)

# include <stdio.h>

/* Function to swap values at two pointers */
void swap (char *x, char *y)
{
     char temp;
     temp = *x;
     *x = *y;
     *y = temp;
}

/* Function to print permutations of string
    This function takes three parameters:
    1. String
    2. Starting index of the string
    3. Ending index of the string. */
void permute(char *a, int i, int n)
{
    int j;
    if (i == n)
      printf("%s\n", a);
    else
    {
          for (j = i; j <= n; j++)
         {
             swap((a+i), (a+j));
             permute(a, i+1, n);
             swap((a+i), (a+j)); //backtrack
         }
    }
}

/* Driver program to test above functions */
int main()
{
    char a[] = "ABC";
    permute(a, 0, 2);
    getchar();
    return 0;
}

答案 7 :(得分:0)

def perms(s):
    if len(s) < 1:
        return [s]
    ps = []
    for i in range(0, len(s)):
        head, tail = s[i], s[0:i] + s[i + 1:]
        ps.extend([head + tailp for tailp in perms(tail)])
    return ps

答案 8 :(得分:-1)

我在GeeksForGeeks分析了这个回溯算法的执行时遇到了同样的问题。亲自看看它是如何执行的。有时打印值确实有助于可视化程序的执行

swap((a+l), (a+i)); ~~~~ Forward swap the //////// i= 0 //////// l = 0 //////// r = 2 //////// string = ABC
permute(a, l+1, r); ==== Values send to permute i.e. string= ABC //////// (l+1) = 1 //////// r = 2
swap((a+l), (a+i)); ~~~~ Forward swap the //////// i= 1 //////// l = 1 //////// r = 2 //////// string = ABC
permute(a, l+1, r); ==== Values send to permute i.e. string= ABC //////// (l+1) = 2 //////// r = 2
ABC
______________________________________________________________________
swap((a+l), (a+i));++++ Backtract swap //////// i= 1 //////// l = 1 //////// r = 2 //////// string = ABC
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
------------------------------END OF BACKTRACK--------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
swap((a+l), (a+i)); ~~~~ Forward swap the //////// i= 2 //////// l = 1 //////// r = 2 //////// string = ACB
permute(a, l+1, r); ==== Values send to permute i.e. string= ACB //////// (l+1) = 2 //////// r = 2
ACB
______________________________________________________________________
swap((a+l), (a+i));++++ Backtract swap //////// i= 2 //////// l = 1 //////// r = 2 //////// string = ABC
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
------------------------------END OF BACKTRACK--------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
swap((a+l), (a+i));++++ Backtract swap //////// i= 0 //////// l = 0 //////// r = 2 //////// string = ABC
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
------------------------------END OF BACKTRACK--------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
swap((a+l), (a+i)); ~~~~ Forward swap the //////// i= 1 //////// l = 0 //////// r = 2 //////// string = BAC
permute(a, l+1, r); ==== Values send to permute i.e. string= BAC //////// (l+1) = 1 //////// r = 2
swap((a+l), (a+i)); ~~~~ Forward swap the //////// i= 1 //////// l = 1 //////// r = 2 //////// string = BAC
permute(a, l+1, r); ==== Values send to permute i.e. string= BAC //////// (l+1) = 2 //////// r = 2
BAC
______________________________________________________________________
swap((a+l), (a+i));++++ Backtract swap //////// i= 1 //////// l = 1 //////// r = 2 //////// string = BAC
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
------------------------------END OF BACKTRACK--------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
swap((a+l), (a+i)); ~~~~ Forward swap the //////// i= 2 //////// l = 1 //////// r = 2 //////// string = BCA
permute(a, l+1, r); ==== Values send to permute i.e. string= BCA //////// (l+1) = 2 //////// r = 2
BCA
______________________________________________________________________
swap((a+l), (a+i));++++ Backtract swap //////// i= 2 //////// l = 1 //////// r = 2 //////// string = BAC
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
------------------------------END OF BACKTRACK--------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
swap((a+l), (a+i));++++ Backtract swap //////// i= 1 //////// l = 0 //////// r = 2 //////// string = ABC
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
------------------------------END OF BACKTRACK--------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
swap((a+l), (a+i)); ~~~~ Forward swap the //////// i= 2 //////// l = 0 //////// r = 2 //////// string = CBA
permute(a, l+1, r); ==== Values send to permute i.e. string= CBA //////// (l+1) = 1 //////// r = 2
swap((a+l), (a+i)); ~~~~ Forward swap the //////// i= 1 //////// l = 1 //////// r = 2 //////// string = CBA
permute(a, l+1, r); ==== Values send to permute i.e. string= CBA //////// (l+1) = 2 //////// r = 2
CBA
______________________________________________________________________
swap((a+l), (a+i));++++ Backtract swap //////// i= 1 //////// l = 1 //////// r = 2 //////// string = CBA
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
------------------------------END OF BACKTRACK--------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
swap((a+l), (a+i)); ~~~~ Forward swap the //////// i= 2 //////// l = 1 //////// r = 2 //////// string = CAB
permute(a, l+1, r); ==== Values send to permute i.e. string= CAB //////// (l+1) = 2 //////// r = 2
CAB
______________________________________________________________________
swap((a+l), (a+i));++++ Backtract swap //////// i= 2 //////// l = 1 //////// r = 2 //////// string = CBA
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
------------------------------END OF BACKTRACK--------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
swap((a+l), (a+i));++++ Backtract swap //////// i= 2 //////// l = 0 //////// r = 2 //////// string = ABC
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
------------------------------END OF BACKTRACK--------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------