c - levenshtein 总是无限循环递归C

标签 c recursion levenshtein-distance

列支敦士登在c编程中总是返回无限循环这是我的代码我尝试了很多解决方案并且我尝试存储变量并使用指针但总是有无限循环我认为这是因为3个递归调用但在列支敦士登算法的文档中我找到了这个实现

#include "distance.h"
#include "sequence.h"
#include "string.h"
#include <stdarg.h>
int max(int a, int b){
    if (a>b) return a;
    return b;
}

float min(float a,float b, float c){
    if((a<b) && (a<c)) return a;
    if((c<b) && (c<a)) return c;
    return b;
}

float dif(int a,int b){
    int d;
    d = a-b;
    if((a==32)||(b==32)) return 1.5;  //verification d'espaceC

    if(d==0.0) return d;
    if((abs(d)==17.0) || (abs(d)==6.0)) return 1.0;
    return 2;
}

float distance_D1(SEQUENCE S1, SEQUENCE S2){ 
    float d = 0.0; int a,b; int m; int i;
    m = max(S1.l,S2.l);
    for(i=0;i < m; i++){
        a = S1.tc[i];
        b = S2.tc[i];
        d = d + dif(a,b) ;
        printf("%.1f\n",d);
    }
    return d;
}



float DistanceMinRec(char* a,char* b,int n,int m){
 printf("%s \n",a);
  printf("%s \n",b);
  printf("%d \n",n);
  printf("%d \n",m);
 float l,k,j,d;

       if (m <= 0) return n; 

    // If second string is empty, the only option is to 
    // remove all characters of first string 
    if (n <= 0) return m; 

    // If last characters of two strings are same, nothing 
    // much to do. Ignore last characters and get count for 
    // remaining strings. 

    if (a[m] == b[n]) {
        return DistanceMinRec(a, b, m-1, n-1); 
  }
    // If last characters are not same, consider all three 
    // operations on last character of first string, recursively 
    // compute minimum cost for all three operations and take 
    // minimum of three values. 
    l=DistanceMinRec(a,  b, m, n-1)+dif(a[m],b[n-1]);
    k=DistanceMinRec(a,  b, m-1, n)+dif(a[m-1],b[n]);
    j=DistanceMinRec(a,  b, m-1, n-1)+dif(a[m-1],b[n-1]);
    d= min (l ,    // Insert 
                     k,   // Remove 
                   j   // Replace 
                   );
                   return d; 
}

主文件

int n=strlen(S1.tc);
    int m=strlen(S2.tc);
    char* X = S1.tc;  
    char* Y = S2.tc;
    // char* X = "sunday";  
    // char* Y =  "saturday";

    //affiche_sequence(S1);
    //affiche_sequence(S2);
    d = DistanceMinRec(X,Y,n,m);
    printf("Distance entre %s et %s = %.1f\n", argv[1],argv[2],d);

    exit(0);

最佳答案

我认为变量的刷新是问题所在,我在递归函数中注释了 printf block ,它可以工作,但我不确定这是正确的输出

关于c - levenshtein 总是无限循环递归C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58848186/

相关文章:

javascript - JavaScript 中的递归调用不起作用?

c# - 递归类的最佳实践

python - 如何加速 itertools 组合?

python - 你怎么知道广播消息来自哪个接口(interface)?

java - 用递归方法求和值

c - 数组中质数的平均值

PHP:使用 levenshtein 距离来匹配单词

javascript - 距索引 0 的 Levenshtein 距离

c++ - 两个文件中相同函数/全局变量的不同声明

c - 如何在Python代码中使用C中的函数,该函数将函数回调作为来自dll文件的参数?