levenshtein 总是无限循环递归 C
levenshtein ALWAYS infinite loop recursive C
C 编程中的列支敦士登总是 return 无限循环这是我的代码我尝试了很多解决方案并且我尝试存储变量并使用指针但我总是有无限循环我认为这是因为 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 块并且它有效但我不确定这是正确的输出
C 编程中的列支敦士登总是 return 无限循环这是我的代码我尝试了很多解决方案并且我尝试存储变量并使用指针但我总是有无限循环我认为这是因为 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 块并且它有效但我不确定这是正确的输出