动态规划法:

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

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. Robot Framework--BuiltIn库4

    Catenate :关键字可以连接多个信息. Create List :关键字可以定义列表. get time :获取当前时间. Evaluate :数值运算并得到结果: Should系列关键字是Sh ...

  2. Nmap UDP扫描缓慢问题探究(无结果)

    一.说明 在网络原理中我们经常说TCP是面向连接的要进行三次握手和四次挥手所以速度比较慢,UDP是无连接的直接发送和接收所以速度快(说到这个快慢我总想起多年前有篇分析MSN为什么被QQ淘汰的一篇文章其 ...

  3. c# 使用Renci.SshNet.dll操作SFTP总结

    1.操作类 /// <summary> /// SFTP操作类 /// </summary> public class SFTPHelper { #region 字段或属性 p ...

  4. 实现我的第一个Java程序

    第一步.打开记事本 第二步.代码编写 public class Hello{ public static void main( String[] args){ System.out.println(& ...

  5. Frist one

    2017.11.27 10:20am 今天也许只是普通的一个周一上午 但是我希望在多年以后 在我回望这些年的努力学习中 今天会是最浓墨重彩的一笔 我时常感到焦虑 对未来的焦虑 对感情的焦虑 以至于 我 ...

  6. tp5.0中使用PHPexcel,以及Loader的一些问题

    在5.0中使用PHPexcel遇到一些问题,记录一下方便以后查看. 非使用PHPexcel的方法: 参考:http://blog.csdn.net/sinat_35861727/article/det ...

  7. CentOS6.5 - yum对Mysql的安装与配置

    一.mysql的安装 1.查看是否安装mysql [root@localhost ~]# rpm -qa | grep mysql 如果有进行卸载(以下三种方式选一种即可): -.el6.x86_64 ...

  8. SQLZOO网页中SQL的答案(SELECT from world篇)

    SELECT from world篇 11. 题目: The CASE statement shown is used to substitute North America forCaribbean ...

  9. Sql Server 2012 集群配置

    基于Windows Server 2008 R2的WSFC实现SQL Server 2012高可用性组(AlwaysOn Group) 2012年5月 微软新一代数据库产品SQL Server 201 ...

  10. UGUI血条

    using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI; pu ...