- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我找到了很多确定两个字符串之间编辑距离 (LD) 的来源。然而,它们都假设替换、插入和删除操作的成本都设置为 1。
这source for C++ 非常高效,我正在尝试在下面进行调整以允许每次操作的不同成本。
#include <vector>
#include <string>
#include <iostream>
size_t levenshtein_distance(const std::string &a, const std::string &b);
int main()
{
std::string a, b;
a = "roger"; b = "Roger";
std::cout << levenshtein_distance(a, b) << std::endl;
a = "roger"; b = "oger";
std::cout << levenshtein_distance(a, b) << std::endl;
a = "oger"; b = "roger";
std::cout << levenshtein_distance(a, b) << std::endl;
return 0;
}
size_t levenshtein_distance(const std::string &a, const std::string &b)
{
// Costs of operations
const size_t substitution = 5;
const size_t deletion = 2;
const size_t insertion = 2;
size_t a_size = a.size(), b_size = b.size();
std::vector<size_t> P(b_size + 1), Q(b_size + 1);
for (size_t i = 0; i < Q.size(); i++)
Q[i] = i;
for (size_t i = 0; i < a_size; i++)
{
P[0] = i + 1;
for (size_t j = 0; j < b_size; j++)
P[j + 1] = std::min(
std::min(Q[j + 1] + 1, P[j] + 1), Q[j] + ((a[i] == b[j])? 0: substitution));
P.swap(Q);
}
return Q[b_size];
}
我想我在正确的地方有substitution
。如果我将它更改为 5,它会为该操作提供相应大的 LD,但似乎无法找到在哪里应用 insertion
或 deletion
。我尝试更改文字 1
,但它们似乎没有任何效果 - 对于插入或删除操作,结果始终为 1。
最佳答案
您可以按如下方式改编来自维基百科的算法:
size_t levenshtein_distance(const std::string &s1, const std::string &s2)
{
const size_t substitution = 5;
const size_t deletion = 2;
const size_t insertion = 3;
const size_t len1 = s1.size(), len2 = s2.size();
vector<vector<unsigned int> > d(len1 + 1, vector<unsigned int>(len2 + 1));
d[0][0] = 0;
for(unsigned int i = 1; i <= len1; ++i) d[i][0] = deletion * i;
for(unsigned int i = 1; i <= len2; ++i) d[0][i] = insertion * i;
for(unsigned int i = 1; i <= len1; ++i)
for(unsigned int j = 1; j <= len2; ++j)
d[i][j] = std::min(
std::min(d[i - 1][j] + deletion, d[i][j - 1] + insertion),
d[i - 1][j - 1] + (s1[i - 1] == s2[j - 1] ? 0 : substitution)
);
return d[len1][len2];
}
例如d[i][0]
表示将第一个字符串的前i
个字符转换为第二个字符串的零个第一个字符的成本。这显然来自删除,所以 d[i][0] = deletion * i
。同样,d[0][i] = insertion * i
。
如果不想使用二维数组,那么只能保留最后一行:
int levenshtein_distance(const std::string &s1, const std::string &s2)
{
const int substitution = 5;
const int deletion = 2;
const int insertion = 3;
const int len1 = s1.size(), len2 = s2.size();
std::vector<int> P(len2 + 1), Q(len2 + 1);
for(int j = 1; j <= len2; ++j) P[j] = insertion * j;
for(int i = 1; i <= len1; ++i) {
Q[0] = deletion * i;
for(int j = 1; j <= len2; ++j) {
Q[j] = std::min(Q[j - 1] + insertion, P[j] + deletion);
Q[j] = std::min(Q[j], P[j - 1] +
(s1[i - 1] == s2[j - 1] ? 0 : substitution)
);
}
std::swap(P, Q);
}
return P[len2];
}
关于c++ - 修改现有的 Levenshtein 距离代码以适应不同的操作成本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29017600/
我遇到了问题,似乎无法解决。 基本上,我希望我的菜单和文本随着分辨率的变化/放大/缩小而调整大小;图片工作正常,内容 div 工作正常,只是菜单似乎无法适应。 请帮我解决这个问题.. 普通 View
我在map reduce上开发了大约20个工作,包括pagerank算法。我从未发现任何具有挑战性的问题可以在线适应mapreduce框架。我想提高自己的技能。有这样的指南吗? 最佳答案 您正在寻找的
我了解到 Java 中没有 NOR,我可以使用 !A && !B 或 !(A||B)。既然 A 和 B 都是假的,为什么我不能使用 !A || B? 最佳答案 因为Java没有NOR运算符,你的表达式
我需要根据构建 CLI 工具的要求评估“GO”,该工具应该可以在不同的操作系统中运行。这是如何在诸如“Cloud Foundry CLI”之类的 CLI 工具中实现的?GO 如何处理这种对操作系统的适
所以我试图让我的下拉菜单正常工作。下拉列表位于我的标题中的列表项下,该列表项会根据用户的用户名而变化。这使得大小不同,但我无法让下拉菜单跟随宽度调整大小。 示例 1: http://jsfiddle.
我已经很努力了,但我还是被 matplotlib 卡住了。请忽略,mpl 文档让我有点困惑。我的问题涉及以下方面: 我用 matshow 函数画了一个对称的 n*n 矩阵 D。行得通。 我想做同样的事
我正在尝试调整 Boyer-Moore c(++) Wikipedia implementation获取字符串中模式的所有匹配项。实际上,维基百科实现返回第一个匹配项。主要代码如下: char* bo
我在底部嵌套了带有文本的 flex 元素。顶部元素具有小于文本的固定宽度: .list-header { display: flex; width: 150px; height: 80px
我想初始化 std::any带有仅 move 类型变量。我找到了 Cannot move std::any . 编译错误案例 在使用链接答案中的 shared_ptr 解决方法之前,我测试了以下代码:
现在我正在使用 webview 处理 ListView 。在这个 listview webview 中用于显示图像。它来自 url。现在我面临一个问题,我无法从 url 获得唯一尺寸的图像,一些图像很
我的文件夹结构是这样的: src --main.cpp tests --src ----main_test.cpp Makefile 我想制作一个像make main_test这样的目标,以便能够以.
前段时间我也在讨论类似的话题。我正在查看我的应用程序,我认为它有很多不必要的代码。我的意思是我有服务负责从两家书店的不同类别的书籍中抓取数据。现在我有 5 个类别,所以我有 5 个方法,但如果我要添加
我使用多个 div 子元素创建父元素,然后根据 data-value 属性在 JavaScript 中计算这些子元素的宽度。 如果我对所有 child 的计算宽度求和,我最终将得到 100%。但出于某
我像这样使用减速板 gem : require 'airbrake' Airbrake.configure do |config| config.api_key = 'XXXXX' confi
我们在企业环境中有一个 svn 存储库结构,如下所示: root libs shared_lib1 shared_lib2 private_lib public_cod
我制作了一个应用程序,其中有许多从 UIView 子类化的 View 。这些 View 的大小和方向是随机的,并且可以保存应用程序屏幕的状态。当用户在打开屏幕的同一设备上保存屏幕时,屏幕状态为“正常”
我需要调整 lucene 的 StandardTokenizer 以适应有关 twitter 数据的一些特殊目的。目前,我使用 StandardTokenizer 来标记一些我想要处理的推文。它工作得
在Windows Store应用中,如果内容是固定的,我们可以把它封装成一个ViewBox,让内容适应不同的分辨率。但是,如果内容不固定,在我的应用程序中,有一个 GridView 哪些项目是动态的,
一切都在标题中。 在我的应用程序中,根据用户所做的选择,我用一个列表填充一个组合框,该列表有时很小(1 个元素)有时很大(150 个元素)。 我想要的不是在启动时将固定高度设置为给定值,而是将 max
我的 2 div 动画感谢我下面的 jquery 脚本。当屏幕小于 700px 时,div 变成更小的正方形(35px 而不是 50px)。我希望 .animate({width:100px}); 仅
我是一名优秀的程序员,十分优秀!