为什么我的代码没有显示正确的编辑距离命令?

问题描述

我正在编写一个编辑距离程序,该程序将打印正确的命令序列(ADD,DEL,SET)。 这是我的代码

int start(char *path,char *path2) {
char *file1=openFile(path),*file2=openFile(path2);
long int dim1=calcLen(path),dim2 =calcLen(path2);
register int i,j;
int *prev = malloc((dim2 + 1) * sizeof(int));
int *curr = malloc((dim2 + 1) * sizeof(int));
int *tmp = 0;
for(i=0;i<=dim2;i++)prev[i]=i;

//_________________________________________________________________//
for (i = 1; i <= dim1; i++) {
    curr[0] = i;
    for (j = 1; j <= dim2; j++) {

        if (file1[i - 1] != file2[j - 1]) {
            int k = minimum(curr[j - 1],prev[j - 1],prev[j]);
            curr[j] = k + 1;
            if(k==prev[j-1]){
                printf("SET\n");
            }else if(k==prev[j]){
                printf("ADD\n");
            }else if(k==curr[j-1]){
                printf("DEL\n");
            }
        } else {
            curr[j] = prev[j - 1];
        }

    }

    tmp = prev;
    prev = curr;
    curr = tmp;

    memset((void *) curr,sizeof(int) * (dim2 + 1));
  }
}

两个字符串是:

  • 你好
  • 卡梅洛

它打印了我:

SET
SET
SET
SET
SET
SET
SET
SET
SET
SET
SET
SET
DEL
DEL
DEL
DEL
SET
SET
SET
SET
SET
DEL
DEL
SET
SET
SET
SET
SET
DEL
DEL
SET
SET
SET
SET
SET
SET
ADD
DEL

Process finished with exit code 0

但是,如果我使用一种方法来计算这两个字符串之间的距离,则会输出

EDIT disTANCE: 5
TIME: 0.002000
Process finished with exit code 0

我真的不明白它是如何做到的,我认为它会打印很多命令,因为第二个命令是。但是我认为,如果算法错误,那么所有程序都可能是错误的,而不仅仅是方法“启动”。

这是calcLen:

int calcLen(char *path) {
FILE *fp = fopen(path,"r");
if (fp == NULL) {
    printf("File Not Found!\n");
    return -1;
}
fseek(fp,0L,SEEK_END);
long int res = ftell(fp);
fclose(fp);
return res;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)