【问题标题】:Find all possible interpretations of an array of digits查找数字数组的所有可能解释
【发布时间】:2013-09-10 13:46:42
【问题描述】:

考虑一个将字母转换为整数的编码系统,其中“a”表示为 1,“b”表示为 2,..“z”表示为 26。给定一个数字数组(1 到 9)作为输入,编写一个函数打印输入数组的所有有效解释。

/*Examples

Input: {1, 1}
Output: ("aa", 'k") 
[2 interpretations: aa(1, 1), k(11)]

Input: {1, 2, 1}
Output: ("aba", "au", "la") 
[3 interpretations: aba(1,2,1), au(1,21), la(12,1)]

Input: {9, 1, 8}
Output: {"iah", "ir"} 
[2 interpretations: iah(9,1,8), ir(9,18)]*/

我的 c 代码

#include<iostream>
using namespace std;
#include<string.h>
int a[10]={2,3,4,4,2,4,2,8,9};
char c[]={'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'};

void func(int i,char result[10])
{
    if(i==10)
    {
       int l=strlen(result);
       for(int j=0;j<l;j++)
           cout<<result[j];
    }

    else
    {
        if(10*a[i]+a[i+1]<26)
        {
            strcat(result,"c[10*a[i]+a[i+1]]");
            func(i+2,result);
        }

        strcat(result,"c[a[i]]");
        func(i+1,result);
    }
}

int main()
{

    func(0,"");
}

我无法找出错误。你能帮帮我吗?

【问题讨论】:

  • 你看到了什么错误?
  • "using namespace std" 几乎不是“你的 C 代码”。我假设你的意思是 C++?
  • 另外,您正在尝试将 strcat 转换为字符串文字,该文字不够短且只读。
  • Vaughn Cato-- 这是一个运行时错误(分段错误)
  • H2CO3--是的,我的意思是 c++ 而已

标签: c++ algorithm recursion dynamic-programming


【解决方案1】:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int a[9]={2,3,4,4,2,4,2,8,9};
char c[]={'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'};

void func(int i,char result[11])
{
    if(i==10)
    {
      printf ("%s\n", result);
    }
    else
    {
        char * temp = (char *)malloc(11);
        sprintf(temp, "%s%c", result, c[a[i] - 1]);
        func(i+1,temp);
        free(temp);

        if(i < 9 && 10*a[i]+a[i+1] < 26)
        {
            char * temp = (char *)malloc(11);
            sprintf(temp, "%s%c", result, c[10*a[i]+a[i+1] - 1]);
            func(i+2, temp);
            free (temp);
        }
    }
}
int main()
{
    func(0,"");
}

输出

bcddbdbhi
bcddxbhi
wddbdbhi
wddxbhi

您的代码可能存在致命问题

  1. strcat(result,"c[10*a[i]+a[i+1]]");strcat(result,"c[a[i]]"); 您将这些字符串连接到大小仅为 10 的 result,而不是连接与该数字对应的字符。

其他问题

  1. if(10*a[i]+a[i+1]&lt;26)内部你正在改变result的内容,当递归结束时,结果的值与它进入函数时的值不一样。所以最好在内部创建新字符串并在任务结束时释放它们。

建议

  1. 尽量不要混合使用 C 和 C++ 函数。

【讨论】:

    【解决方案2】:

    我看到的一个错误是您的结果数组可能没有 10 个字符。

    for(int j=0;j<10;j++)
    cout<<result[j];
    

    这将导致分段错误。

    编辑:如果您愿意使用 C++ 元素,那么我更喜欢使用 std::string 而不是 char 数组作为结果。

    【讨论】:

      【解决方案3】:

      您询问了错误。如下:

      1. 不要将“使用命名空间”放在其他包含之前,这可能会导致其他项目中的编译器出现非常奇怪的错误。
      2. a[10] 未定义,但在调用 func(9, result) 时进行评估;
      3. 另外,调用 func(9, result) 可能会导致调用 func(9+2, result)。
      4. strcat(结果,"c[10*a[i]+a[i+1]]");将字符串 "c[10*a[i]+a[i+1]]" 添加到结果中。我怀疑这是你想要的结果。此外,编译器应建议您改用 strncat()。
      5. 最好使用 std::string 和 std::stringstream,除非您有充分的理由避免使用它们。
      6. 变量 a 在 func() 中使用,但不作为参数传递。一般来说,这不是好主意。

      示例代码(这使用了一些其他技巧):

          void PrintDecodings(const int* a, size_t length, std::string acc){
              static const char* alphabet = "0abcdefghijklmnopqrstuvwxyz";
              if(length == 0){
                  std::cout << acc << std::endl;
                  return;
              }
              if(length == 1){
                  std::cout << acc << alphabet[*a] << std::endl;
                  return;
              }
              if(10*a[0]+a[1] <= 26){ //At this point, length>=2, so a[1] is OK
                  PrintDecodings(a+2, length-2, acc+alphabet[10*a[0]+a[1]]);
              }
              return PrintDecodings(a+1, length-1, acc+alphabet[*a]);
          }
      
          //...
              PrintDecodings(a, 9, "");
      

      【讨论】:

        【解决方案4】:
        #include<iostream>
        using namespace std;
        #include<string.h>
        int a[3]={1,2,1};
        char c[]={'NULL','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'};
        
        void func(int i,char result[3],int r)
        {
            if(i==3)
            {
              for(int j=0;j<r;j++)
                   cout<<result[j];
               cout<<"\n";
               return;
            }
            if(10*a[i]+a[i+1]<26)
            {
              result[r]=c[10*a[i]+a[i+1]];
              func(i+2,result,r+1);
            }
            result[r]=c[a[i]];
            func(i+1,result,r+1);
        
        }
        
        
        int main()
        {
            char result[10];
            func(0,result,0);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-12-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多