我试图从着名的'diff'文件here中理解算法,在两个命令行参数中运行字符。但是,我的代码不会产生我期望的结果。我编写了算法以尽可能匹配他们的变量:
$ ./diff abbcabba cbabac #Hmm.. I think it should be 5
SES: 10
$ ./diff abcdefg abcdefg #0! Great!
SES: 0
$ ./diff abcXefg abcYefg # 2 seems reasonable
SES: 2
$ ./diff abcXefg abcefg # clearly wrong
SES: 7
这是我的代码(对代码墙感到抱歉):
a = argv[1];
b = argv[2];
alen = strlen(a);
blen = strlen(b);
tlen = alen + blen;
maxd = tlen;
vp = (int *)calloc(2 * maxd + 1, sizeof(int));
// How I'm thinking about pointer arithmetic:
// vp in [0, 2*maxd + 1) == [0, 2*maxd]
// v in [-maxd, maxd + 1) == [-maxd, maxd]
v = vp + maxd;
v[1] = 0;
for (D = 0; D <= maxd; D++) {
for (k = -D; k <= D; k += 2) {
if (k == -D || (k != D && v[k-1] < v[k+1])) {
x = v[k + 1];
} else {
x = v[k - 1] + 1;
}
y = x - k;
while (x < alen && y < blen && a[x] == b[x]) {
x++;
y++;
}
v[k] = x;
if (x >= alen && y >= blen) {
printf("SES: %d\n", D);
goto out;
}
}
}
printf("Nothing found. SES > %d\n", maxd);
知道这里有缺陷吗?我一直觉得难以在网上搜索这个问题......
答案 0 :(得分:4)
似乎问题在于这一行:
while (x < alen && y < blen && a[x] == b[x]) {
此处b[x]
应为b[y]
,其中包含:
while (x < alen && y < blen && a[x] == b[y]) {
通过此更改,您的示例的结果为6,0,2和1.这似乎是准确的。