动态规划法:

用二维矩阵来的每个元素来代表两个字符串的字符匹配情况,

LCS[i, j]= LCS[i-1, j-1] + 1  , if X[i-1] == Y[J-1].

LCS[i, j] =0, everything else

每一个元素记载着两个字符串的相似程度,而且每个元素只需关心上一个元素的值来计算字符串比较的累计情况。

实现代码:


       public static string LongestCommonString(string x, string y)
{
if (x == null || y == null)
{
return null;
} if (string.IsNullOrEmpty(x) || string.IsNullOrEmpty(y))
{
return string.Empty;
} int m = x.Length;
int n = y.Length; int[,] LCS = new int[m, n];
int result = ;
int mIndex = ;
int nIndex = ; for(int i = ; i < m; i++)
{
for (int j = ; j < n; j++)
{
if (i == || j == )
{
LCS[i, j] = ;
}
else if (x[i - ] == y[j - ])
{
LCS[i, j] = + LCS[i - , j - ];
if (result < LCS[i, j])
{
result = LCS[i, j];
mIndex = i;
nIndex = j;
}
}
}
} StringBuilder sb = new StringBuilder();
Stack<char> stack = new Stack<char>();
while(result > )
{
stack.Push(x[mIndex-]);
result = LCS[mIndex-, nIndex-];
mIndex--;
nIndex--;
} while(stack.Count > )
{
sb.Append(stack.Pop());
} return sb.ToString();
}

Find Longest common string的更多相关文章

  1. 14.Longest Common Prefix (String)

    Write a function to find the longest common prefix string amongst an array of strings. class Solutio ...

  2. [LeetCode] Longest Common Prefix 最长共同前缀

    Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...

  3. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  4. 【leetcode】Longest Common Prefix

    题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...

  5. LintCode 78:Longest Common Prefix

      public class Solution { /** * @param strs: A list of strings * @return: The longest common prefix ...

  6. [LintCode] Longest Common Prefix 最长共同前缀

    Given k strings, find the longest common prefix (LCP). Have you met this question in a real intervie ...

  7. SPOJ LCS2 - Longest Common Substring II

    LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...

  8. 14. Longest Common Prefix

    题目: Write a function to find the longest common prefix string amongst an array of strings. Subscribe ...

  9. Leetcode Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. class Solutio ...

随机推荐

  1. DataSet结果转模型类

    引入命名空间: using System.Data; using System.Reflection; 类封装代码: public class ModelHelper { public T To< ...

  2. 移动端input输入placeholder垂直不居中

    在移动端编写input输入框时候,为了输入文字与输入框垂直居中,一般情况下,会将input的line-height的高度等于height.但在移动端输入的时候会发现,虽然输入内容确实是垂直居中了,但是 ...

  3. gevent模块学习(四)

    gevent.spawn会对传入的子任务集合进行调度,gevent.joinall 方法会阻塞当前程序,除非所有的greenlet都执行完毕,才会退出程序 公有方法 gevent.spawn(cls, ...

  4. python - 递归 二分法

    一.一些内置函数 1.revsered  翻转,返回的是迭代器 # 将 s 倒置 s = '不是上海自来水来自海上' # 方法一 print(s[::-1]) # 方法二 s1 = reversed( ...

  5. linux jpg文件查找木马

    find ./ -type f -name "*.jpg" | xargs grep "eval"

  6. element-ui的那些坑与总结

    tags: 默认情况下,下划线是文本宽度 如果要加宽,则可以设置文本(label)的padding, 常规情况下,无法改label宽度,因为他是动态计算的 不过,可以通过自定义,把label拿出来,自 ...

  7. ES6 中 Promise

    在说Promise之前我们先简单说一下什么是同步异步? 同步(Sync):所谓同步,就是发出一个功能调用时,在没有得到结果之前,该调用就不返回或继续执行后续操作. 异步(Async):异步与同步相对, ...

  8. python 捕获异常顺序

    catch 异常的时候,有关的异常(若是抛出子类异常,则父类异常的except也算.反之不算)except的语句是按代码顺序执行, 也就是说,当一个异常发生时,从若干except中若遇见异常类基类,父 ...

  9. Day-01

    昨天学习的内容都是一些简单的入门知识 like:二进制,编程语言这些 我觉得二进制还蛮好玩的 对于ascii码 还好,我不是很陌生 因为学函数的时候,老师有讲到这些 嗯 昨天就这些 继续加油~~~

  10. MAC安装flutter开发环境

    #最近在学flutter开发,写一篇记录一下安装的过程 1.配置flutter镜像地址  vim ~/.bash_profile 命令行输入后回车,打开.bash_profile配置镜像地址 expo ...