从数组中创建所有可能的单词

时间:2017-11-28 08:16:08

标签: c arrays recursion

我想创建一个递归函数,它将从数组(字典)中创建所有可能的单词 只是为了澄清,用词我的意思是字符串,例如" aab"在这种情况下是一个词。

char letters[36] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9'};

顺便说一下,我不想要无限量的单词,最大长度为10。

到目前为止,我有这段代码:

char letters[36] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9'};
    char s[10]="";
    s[9]='\0';
    int l=0;
    printf("test \n");
    recur(s,letters,l);

递归函数:

int recur(char * mot , char * tab,int l){
    printf("int : %i \n",l);
    if(l<2) {

        for (int i = 0; i < sizeof(tab); ++i) {
            mot[l]=tab[i];
            printf("%s \n",mot);
            l=l+1;
            recur(mot,tab,l);
        }    
    }
    else {
        return 1;
    }
}

但是从头开始我没有任何问题。

编辑:

结果是希望是这样的:

长度== 1

a
b
c
d
.
.
8
9

长度== 2

aa
ab
ac
.
c7
c8
.
.
99

另一个例子:cd7e长度== 4

修改

我将代码更改为: (测试所有字符串,最大长度= 2)

int recur(char * mot , char * tab,int l){

    if(l<2) {
        for (int i = 0; i < 36; ++i) {
            mot[l]=tab[i];
            printf("%s \n",mot);
            recur(mot,tab,l+1);
        }

    }
    else{
        return 1;
    }
} 

结果是

  

a a ab ac ad ae af ag啊ai ....

它跳过长度为1的字符串,我不知道为什么。

修改

我试过l<3,这就是结果:

  aa aa aa aab aab aad aae aae ...   a91 a92 a93 a94 a95 a96 a97 a98 a99 b99 ba9 baa bab bac   bad bae baf bag bah bai baj bak bal bam ban ...

长度&lt; 3,它跳过长度= 1,2

的所有字符串

2 个答案:

答案 0 :(得分:0)

修复:在每次递归后重置字符串分隔符:

for (int i = 0; i < 36; ++i)
{
    mot[l]=tab[i];
    printf("%s \n",mot);
    recur(mot,tab,l+1);
}
mot[l] = '\0';

发生的是,您从一个空的mot数组开始:

mot = "\0\0\0..."

然后有第一个递归深度:

mot = "a\0\0..."

第二深度(l<2条件的最终深度):

mot = "aa\0..."
...
mot = "a9\0..."

回到第一深度:

mot = "b9\0..."

但你需要:

mot = "b\0\0..."

答案 1 :(得分:0)

您可以使用以下代码。这是完整的工作代码:
[注意:查看working here]

#include <stdio.h>
#include <string.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 l, int r)
{
   int i;
   if (l != r)
   {
       for (i = l; i <= r; i++)
           {
              swap((a+l), (a+i));
              permute(a, l+1, r);
              swap((a+l), (a+i)); //backtrack
           }
   }
   else
   {
       printf("%s\n", a);
   }
}

/* arr[]  ---> Input Array
   data[] ---> Temporary array to store current combination
   start & end ---> Staring and Ending indexes in arr[]
   index  ---> Current index in data[]
   r ---> Size of a combination to be printed */
void combinationUtil(char alphas[], char data[], int start, int end,
                     int index, int count)
{
    int i;
    if (index == count)
    {
        data[count] = '\0';
        permute(data, 0, count-1);
        return;
    }

    for (i=start; i<=end && end-i+1 >= count-index; i++)
    {
        data[index] = alphas[i];
        combinationUtil(alphas, data, i+1, end, index+1, count);
    }
}

// The main function that prints all combinations of size r
// in arr[] of size n. This function mainly uses combinationUtil()
void printCombination(char alphas[], int n, int count)
{
    int data[count+1];
    combinationUtil(alphas, data, 0, n-1, 0, count);
}

int main()
{
    char alphas[] = "ABCD"; //Provide here the characters which you wants to use
    int i;
    int len = strlen(alphas);
    for(i = 0; i<len; i++)
        printCombination(alphas, len, i+1);
    return 0;
}

这是geeksforgeeks的以下链接提供的代码组合:

  • Create Permutation
  • Create Combination