动态规划法:

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

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. 数据结构与算法之PHP排序算法(桶排序)

    一.基本思想 桶排序是将待排序的数据分割成许多buckets,然后每个bucket各自排序,或用不同的排序算法,或者递归的使用bucket sort算法.也是典型的分而治之(divide-and-co ...

  2. hbase-0.92.1表备份还原

    原表结构和数据 hbase(main):021:0* describe 'test' DESCRIPTION ENABLED {NAME => ', TTL = true > ', COM ...

  3. 12、类成员访问修饰符public/private/producted/readonly

    1.private 类的私有成员 private 类的私有成员,只能在内部访问,在外部访问不到,无法被继承,我们可以将不需要被外部修改的定义为私有的 私有成员,只能在内部访问,在外部访问不到 priv ...

  4. mysql 联合表(federated)及视图

    1)验证环境 源库:192.168.8.75 centos 7.5 mysql8.3 目标库:192.168.8.68 redhat 6.8 mysql5.7 2)登录源库并创建源表 $ mysql ...

  5. STL 小白学习(8) set 二叉树

    #include <iostream> using namespace std; #include <set> void printSet(set<int> s) ...

  6. Java基本类型内存字节数

    基本类型 字节数 位(bit) 取值范围 byte 1 1*8 -128~127 short 2 2*8   int 4 4*8   long 8 8*8   float 4 4*8   double ...

  7. git--编写好代码文件后更新到git仓库流程

    先说一下git仓库分类: Git分为三大部分存储区域1:工作区域(就是你打开编辑器的本地代码仓库)2:提交缓存区域(使用git add 命令暂时放置的区域)3:git远程仓库(使用git push命令 ...

  8. Vue入门笔记(二)--基础部分之条件渲染

    github地址:https://github.com/iTao9354/basicVue/tree/master/conditional%20rendering(demo01-03) 一.v-if ...

  9. Java 自增原理

    很多人都知道 i++ 和 ++i 的区别 a = i++: a = i; i = i+1; a = ++ i; i = i + 1; a = i; 但碰到 i = i ++;的时候很多人就懵了? i是 ...

  10. laravel 记录

    1.处理ajax跨域  使用  composer require barryvdh/laravel-cors