我正在研究使用 APARAPI 实现 Levenshtein 距离算法的可能性,但我遇到了一些限制问题- 特别是我需要在内核中创建一个被禁止的数组。
有没有办法解决这个问题,或者更好的是有没有人有一种适用于 APARAPI 的 Levenshtein 距离方法?
附加代码只是为了尝试对 APARAPI 内容进行排序,我知道我没有对结果做任何事情,我现在只是执行一次。
Kernel kernel = new Kernel() {
@Override
public void run() {
ld("hello", "heya");
}
public int ld(String s, String t) {
int d[]; // matrix
int n; // length of s
int m; // length of t
int i; // iterates through s
int j; // iterates through t
int s_i; // ith character of s
int t_j; // jth character of t
int cost; // cost
// Step 1
n = s.length();
m = t.length();
if (n == 0) {
return m;
}
if (m == 0) {
return n;
}
int firstSize = n+1;
d = new int[firstSize*(m + 1)]; //THIS ISN'T ALLOWED
// Step 2
for (i = 0; i <= n; i++) {
d[firstSize*i+0] = i;
}
for (j = 0; j <= m; j++) {
d[firstSize*0+j] = j;
}
// Step 3
for (i = 1; i <= n; i++) {
s_i = s.charAt(i - 1);
// Step 4
for (j = 1; j <= m; j++) {
t_j = t.charAt(j - 1);
// Step 5
if (s_i == t_j) {
cost = 0;
} else {
cost = 1;
}
// Step 6
int a = d[firstSize*(i - 1)+j] + 1;
int b = d[firstSize*i+(j - 1)] + 1;
int c = d[firstSize*(i - 1)+(j - 1)] + cost;
int mi;
mi = a;
if (b < mi) {
mi = b;
}
if (c < mi) {
mi = c;
}
d[firstSize*i+j] = mi;
}
}
return d[firstSize*n+m];
}
};
kernel.execute(1);