实现Oracle的编辑距离相似度函数,用C语言实现
时间: 2023-05-27 08:04:15 浏览: 96
以下是C++实现
```c++
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1e3 + 5;
int ed[maxn][maxn];
int min(int a, int b, int c) {
return min(min(a, b), c);
}
double edit_dist(char* s1, char* s2) {
int len1 = strlen(s1);
int len2 = strlen(s2);
ed[0][0] = 0;
for (int i = 1; i <= len1; i++) ed[i][0] = i;
for (int j = 1; j <= len2; j++) ed[0][j] = j;
for (int i = 1; i <= len1; i++) {
for (int j = 1; j <= len2; j++) {
ed[i][j] = min(ed[i - 1][j] + 1, ed[i][j - 1] + 1, ed[i - 1][j - 1] + (s1[i - 1] == s2[j - 1] ? 0 : 1));
}
}
int max_len = max(len1, len2);
return 1 - 1.0 * ed[len1][len2] / max_len;
}
int main() {
char s1[maxn], s2[maxn];
while (cin >> s1 >> s2) {
printf("%.2f\n", edit_dist(s1, s2));
}
return 0;
}
```
阅读全文