首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >返回不停止功能,递归函数问题?(编程练习,动态编程,Levenshtein回溯)

返回不停止功能,递归函数问题?(编程练习,动态编程,Levenshtein回溯)
EN

Stack Overflow用户
提问于 2019-12-15 18:22:09
回答 1查看 75关注 0票数 0

printOptimalAlignment函数行为不当。当函数到达位置(1,1)时,goto和return不会退出.在它应该结束的地方,没有坠毁,它似乎停留在一个任意的位置(6,6).因为由于某种原因,即使int yL,int xL的值没有增量-er,但如果它到达函数的末尾而不对if语句进行任何“命中”,那么它也会递增(但我不理解为什么它会调用自己。

完整代码:https://repl.it/@fulloutfool/Edit-Distance

代码语言:javascript
复制
void printOptimalAlignment(int** arr, string y, string x,int yL, int xL){
  int I_weight=1, D_weight=1, R_weight=1;
  bool printinfo_allot = 1,printinfo = 1; 

  if(printinfo_allot){
    cout<<"Location: "<<"("<<xL<<","<<yL<<")"<<"-------------------------------\n";
    cout<<"Same check Letters: "<<x[xL-2]<<","
      <<y[yL-2]<<"("<<(x[xL-2] == y[yL-2])<<")"<<"\n";
    cout<<"LL: "<<"("<<xL-1<<","<<yL<<")"
      <<":"<<arr[yL][xL-1]
      <<":"<<(arr[yL][xL-1]+I_weight)
      <<":"<<(arr[yL][xL])
      <<":"<<(((arr[yL][xL-1]+I_weight) == arr[yL][xL])==1)
      <<":"<<(yL>=1 && xL>=1)<<"\n";
    cout<<"xL state:"<<((&x[xL]))<<":"<<(x[xL-1])<<"\n";
    cout<<"yL state:"<<((&y[yL]))<<":"<<(y[yL-1])<<"\n";
    string tx = &x[xL];
    cout<<x.length()<<","<<(tx.length()+1)<<"\n";
  }

  string tx = &x[xL]; // slopy hotfix
  if(x.length()==(tx.length()+1)){
    cout<<"return functionality not working?-=-=-=-=-=-=-=-=\n";
    cout<<"-> Prep last, current distance = "<<arr[yL][xL] <<"\n";
    return;
    //printOptimalAlignment(arr,y,x,yL-1,xL-1);
    //cant use this goto... but where does it go?
    //goto because_Im_a_terrible_person;
    throw "how?... breaking rules... make it stop";
  }

  if(yL>=1 && xL>=1 && (x[xL-2] == y[yL-2]) == 1){
    if(printinfo){
      cout<<"-> Same (same char), current distance = "<<arr[yL][xL] <<"\n";
    }
    printOptimalAlignment(arr,y,x,yL-1,xL-1);
  }

  if(yL>=1 && xL>=1 && (arr[yL-1][xL-1] == arr[yL][xL])){
    if(printinfo){
      cout<<"-> Swap (same int), current distance = "<<arr[yL][xL] <<"\n";
      if(arr[yL-1][xL-1]==0)cout<<"---this is last---\n";
    }
    printOptimalAlignment(arr,y,x,yL-1,xL-1);
  }

  if(yL>0 && xL>0 && (arr[yL-1][xL]+D_weight == arr[yL][xL])){
    if(printinfo){
      cout<<"-> Delete, current distance = "<<arr[yL][xL]<<"\n";
    }
    printOptimalAlignment(arr,y,x,yL-1,xL);
  }

  //really weird ((yL>1 && xL>1) && (((arr[yL][xL-1]+I_weight) == arr[yL][xL])==1))
  //not true if it is?
  bool seperate = (((arr[yL][xL-1]+I_weight) == arr[yL][xL])==1);
  if(yL>=1 && xL>=1){
    if((((arr[yL][xL-1]+I_weight) == arr[yL][xL])==1) && (true)){
      if(printinfo){
        cout<<"-> Insert, current distance = "<<arr[yL][xL]<<"\n";
        cout<<"Next Location1: "<<"("<<xL-1<<","<<yL<<")"<<"\n";
      }
      printOptimalAlignment(arr,y,x,yL,xL-1);
      return;
      //how does it get here... also return gets ignored... prob another stack issue
      cout<<"insert function broke?????? @ (1,1) ???????????????\n";
      //return;

    }
  }
  return;
  cout<<"END... Hopefully.. if you see this Something went wrong\n";
  because_Im_a_terrible_person:
  cout<<"QUIT\n";
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-15 18:58:04

我怀疑您的问题是,您的函数调用了自己,并且您似乎没有考虑到在调用完之后接下来应该发生什么。所以你到了最后的状态,你说return不工作,但它确实.它只是返回到以前对printOptimalAlignment的调用中停止的地方,在返回到调用方之前仍然可能会做一些事情,依此类推。您有三个不同的站点,在这些站点中,递归调用printOptimalAlignment时,没有紧接着返回语句,在任何一个站点上,代码都可能会继续并触发另一个条件块。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59346820

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档