编辑距离是速度很快的算法吗?
$ time a.out ellip--
ellipse
eclipse ellipsis
allied cellist ecliptic elapse elicit elide elite elixir enlist enliven hellish illicit jellied lipid lippy lollipop selling slippy telling
ailing albino alcopop alias alibi alien alight align alike alive allay allege alleged allegro allergy alley alliance allot allow alloy
abide ability ablaze abolish abridge absinth achieve acidic acidly acting action active acuity addict adieu adios adland admiral admire admired
aargh aback abacus abalone abandon abase abashed abate abaya abbess abbey abbot abdicate abdomen abduct abed abet abhor abiding abject
real 0m0.009s
user 0m0.008s
sys 0m0.001s
求了ellip--与最多32,381个词的编辑距离。点〔这里〕下载,words.inc.7z 83K
inline int min3 (int a, int b, int c) { int min = a; if (b < min) min = b; if (c < min) min = c; return min + 1; } int edit_distance(const char *str1, int len1, const char *str2, int len2) { static int dp[32][32]; // 把str1的前i个字符转换为""需要i次删除 for (int i = 0; i <= len1; i++) dp[i][0] = i; // 把""转换为str2的前j个字符需要j次插入 for (int j = 0; j <= len2; j++) dp[0][j] = j; for (int i = 1; i <= len1; i++) { for (int j = 1; j <= len2; j++) { if (str1[i - 1] == '-' || str1[i - 1] == str2[j - 1]) dp[i][j] = dp[i - 1][j - 1]; // 相同字符无需操作 else dp[i][j] = min3( dp[i - 1][j], // 删除 dp[i][j - 1], // 插入 dp[i - 1][j - 1] // 替换 ); } } return dp[len1][len2]; } vector<const char*> r[6]; int main (int argc, char** argv) { if (argc != 2) return 0; int len1 = strlen(argv[1]); for (int i = 0; i < sizeof(words) / sizeof(words[0]); i++) { int len2 = strlen(words[i]); if (abs(len1 - len2) > 4) continue; int d = edit_distance(argv[1], len1, words[i], len2); if (d < 6) r[d].push_back(words[i]); } for (int i = 0; i < 6; i++) { int n = r[i].size(); if (n > 20) n = 20; for (int j = 0; j < n; j++) printf("%s ", r[i][j]); if (n) puts(""); } return 0; } #include <stdio.h> #include <stdlib.h> #include <string.h> #include "words.inc" #include <vector> using namespace std;
for (int i = 0; i < 6; i++) r[i].reserve(10000); 没效果。而华容道里set还是hashset reserve后效果显著。

浙公网安备 33010602011771号