Print all the permutations of a string in C

前端 未结 8 1384
自闭症患者
自闭症患者 2020-11-29 04:06

I am learning backtracking and recursion and I am stuck at an algorithm for printing all the permutations of a string. I solved it using the bell algorithm for permutation b

相关标签:
8条回答
  • 2020-11-29 04:57
    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;
    }
    
    0 讨论(0)
  • 2020-11-29 04:58

    The code has 2 problems, both related to n, the assumed length of the string. The code for (j = i; j <= n; j++) { swap((a+i), (a+j)); ... swap in string's null character '\0' and gives code truncated results. Check the original (i == n) which should be (i == (n-1)).

    Backtracking is applied by calling swap() twice effective undoing its original swap.

    The order of complexity is the same for Bell Algorithm.

    #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));
    
    0 讨论(0)
提交回复
热议问题