【问题标题】:recursive function - subsequence递归函数 - 子序列
【发布时间】:2018-12-29 13:52:23
【问题描述】:

我正在尝试获取一个递归函数,它应该比较两个 int 数组并说 array2 是 array1 的子序列。 我现在的问题是我不知道何时设置布尔递归函数的返回值。 我很高兴得到每一个帮助。

int array1 [] = {1 ,2 ,3 ,4 ,5 ,6,7,8,9,10};
int array2 [] = {2,4,6,8};
int l1 = 10;
int l2 = 4;


bool isSubset ( int p1 , int p2) {

   while (p1 <= l1) {
       if (array1[p1] == array2[p2]) {
            isSubset(p1 + 1, p2 + 1);
        } else {
            isSubset(p1 + 1, p2);
        }
    }
}


int main (void) {
    if ( isSubset(0,0))
        cout << "yes" << endl ;
    else
        cout << " no " << endl ;

    return 0;
}

【问题讨论】:

    标签: c++ recursion boolean subsequence


    【解决方案1】:

    如果您找到了array2 的所有项目(即如果p2 == l2),则返回true。如果您已经到达array1 的末尾,但您还没有找到array2 的一些项目,请返回false
    否则,收集isSubset的所有递归调用结果,如果至少有一个true,则返回true
    代码:

    bool isSubset ( int p1 , int p2) {
        if(p2 >= l2) return true; //  we have found all array2's items
        if(p1 >= l1) return false; 
        bool result = false;
    
        while (p1 < l1 && !result) { // whether we have found the subsequence - break the loop
            if (array1[p1] == array2[p2]) {
                result |= isSubset(p1 + 1, p2 + 1);
            } else {
                result |= isSubset(p1 + 1, p2);
            }
            ++p1;
        }
        return result; 
    } 
    

    这里不需要while循环,这也可以:

    bool isSubset ( int p1 , int p2) {
        if(p2 >= l2) return true; //  we have found all array2's items
        if(p1 >= l1) return false; 
        if(array1[p1] == array2[p2]){
            return isSubset(p1 + 1, p2 + 1);
        }else{
            return isSubset(p1 + 1, p2);
        }
    } 
    

    甚至更短:

    bool isSubset ( int p1 , int p2) {
        if(p2 >= l2) return true; //  we have found all array2's items
        if(p1 >= l1) return false; 
        return isSubset(p1 + 1 , p2 + (array1[p1] == array2[p2]));
    } 
    

    附言isSubset 在这种情况下是坏名,因为这个算法是顺序敏感的。

    【讨论】:

      【解决方案2】:
      #include<bits/stdc++.h>
      using namespace std;
      
      int subs(string input,string output[]){
          if(input.length()==0){
              output[0]="";
              return 1;
          }
          string smallstring=input.substr(1);
          int smallans=subs(smallstring,output);
          for(int i=0;i<smallans;i++){
              output[i+smallans]=input[0]+output[i];
          }
          return 2*smallans;
      }
      
      int main(){
          string input;
          cin>>input;
          int len=input.length();
          int osize=pow(2,len);
          string *output=new string[osize];
          
          int count=subs(input,output);
          for(int i=0;i<count;i++){
              cout<<output[i]<<endl;
          }
      }
      

      【讨论】:

      • 这里我们返回给定字符串的所有子字符串,我认为这是最简单的解决方案。
      • 请阅读:Why should I not #include <bits/stdc++.h? 然后,编辑您的代码以包含所需的 standard 标头 - 否则,您可能会吸引反对票。
      猜你喜欢
      • 2016-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-12
      • 2019-05-13
      • 1970-01-01
      相关资源
      最近更新 更多