首页 > 解决方案 > return 不停止函数,递归函数问题?(编程练习、动态编程、Levenshtein Back-trace)

问题描述

printOptimalAlignment 函数行为异常。当函数到达位置(1,1)时,goto 和 return 不会退出......它应该结束的地方,没有崩溃,它似乎停在(6,6)的任意位置......因为由于某种原因它在即使值 int yL, int xL 没有增量,函数的结尾(但我不明白为什么它在函数末尾没有任何“命中”时调用自身 if陈述。

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

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";
}

标签: c++recursionlevenshtein-distancebacktrace

解决方案


我怀疑你的问题是你的函数调用了自己,而你似乎没有考虑到调用自身完成后接下来会发生什么。因此,您可以到达您所说的return不起作用的情况下的结束条件,但是它确实...它只是返回您在上一个电话中停止的地方,printOptimalAlignment在返回呼叫者之前,它仍然可能会做些事情,依此类推. 您有三个不同的站点,您可以在其中递归调用这些站点printOptimalAlignment,它们不会立即跟随 return 语句,并且在其中任何一个站点中,代码都可能会继续并触发您的另一个条件块。


推荐阅读