时间:2019-03-17 标签:c#longestcommonwordssample
我正在寻找最长的常用词 C# 实现。我遇到的大多数样本都是逐个字符地进行比较。
换句话说,
string1 = access
string2 = advised
是否应该从函数的
任何示例代码中返回 null 输出?
i am looking for a longest common words c# implementation. Most of the samples i have came across are comparing character by character.
in otherwords,
string1 = access
string2 = advised
should return null output from the function
any sample codes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为这个问题通常被称为最长公共子串问题。维基百科文章包含伪代码,并且可以在 Web 上找到 C# 实现。
I think this problem is usually referred to as the Longest common substring problem. The Wikipedia article contains pseudocode, and C# implementations can be found on the Web.
如果您所说的单词指的是这些字母,并通过标点符号与其他字母分开,请尝试以下操作:
这可能不是最优雅的方式,但它对我有用。 =)
If by word you mean these letter things, seperated from the others by punktuation, try this:
This probably is not the most elegant way to do it, but it works for me. =)
将计算字符数组 LCS 的算法转变为计算其他任何数组(例如单词数组)的算法通常非常简单。你尝试过吗?
如果您需要一些提示,这里是我几年前写的一篇文章,介绍如何在 JScript 中的单词数组上实现最长公共子序列。您应该能够毫无困难地使其适应 C#。
http://blogs.msdn.com/ericlippert/archive/ 2004/07/21/189974.aspx
Turning the algorithm which computes LCS of arrays of characters into one that does it to arrays of anything else -- like, say, an array of words -- is usually pretty straightforward. Have you tried that?
If you need some hints, here's an article I wrote a couple years ago on how to implement Longest Common Subsequence on an array of words in JScript. You should be able to adapt it to C# without too much difficulty.
http://blogs.msdn.com/ericlippert/archive/2004/07/21/189974.aspx
查找字符串中的差异称为最长公共子序列问题。以下是用 C# 编写的 LCS 问题的通用解决方案:
此代码为您提供了一个“差异”矩阵,然后可以使用该矩阵来构造两个输入的差异。有关单元测试和示例用法,请参阅 http://sethflowers.com/2012/01/18/basic-diff-with-a-generic-solution-to-the-longest-common-subsequence-problem.html< /a>.
Finding differences in strings is called the Longest Common Subsequence problem. The following is a generic solution to the LCS problem, written in C#:
This code gives you a "difference" matrix, which can then be used to construct the difference from the two inputs. For unit tests and example usage, see http://sethflowers.com/2012/01/18/basic-diff-with-a-generic-solution-to-the-longest-common-subsequence-problem.html.