Find Longest common string
动态规划法:
用二维矩阵来的每个元素来代表两个字符串的字符匹配情况,
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的更多相关文章
- 14.Longest Common Prefix (String)
Write a function to find the longest common prefix string amongst an array of strings. class Solutio ...
- [LeetCode] Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- 【leetcode】Longest Common Prefix
题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...
- LintCode 78:Longest Common Prefix
public class Solution { /** * @param strs: A list of strings * @return: The longest common prefix ...
- [LintCode] Longest Common Prefix 最长共同前缀
Given k strings, find the longest common prefix (LCP). Have you met this question in a real intervie ...
- SPOJ LCS2 - Longest Common Substring II
LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...
- 14. Longest Common Prefix
题目: Write a function to find the longest common prefix string amongst an array of strings. Subscribe ...
- Leetcode Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. class Solutio ...
随机推荐
- vue中$refs的使用
vue中$refs获取组件或元素: 获取的元素就相当于是一个原生获取的元素,可以进行操作 this.$refs.ele.style.color = 'red
- 小谈对Python的认知与期望
18级新生,在大学之前并未接触过程序语言编程,在众多语言编程中只对C语言有个名字上认知.在上个学期初次了解到Python语言,计算机老师表示Python是现在编程语言中如雨后春笋般的发展飞速的计算机语 ...
- CSS3-3
关于背景 一. 渐变&径向渐变(background-image: -webkit-linear-gradient() && background-image: -webkit ...
- 【转载】Excel 三维地图入门
三维地图入门(office 2016) https://support.office.com/zh-cn/article/%E4%B8%89%E7%BB%B4%E5%9C%B0%E5%9B%BE%E5 ...
- bluemix创建docker容器
简介: bluemix是基于kubernetes来服务的免费云空间.绑定信用卡后可以创建一个月的集群,一个月后会被删除. 下面示例介绍如何使用kubernetes dashboard来创建一个容器,并 ...
- RockerMQ实战之快速入门
文章目录 RocketMQ 是什么 专业术语 Producer Producer Group Consumer Consumer Group Topic Message Tag Broker Name ...
- DevExpress v18.2版本亮点——Reporting篇(三)
行业领先的.NET界面控件——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍新版本新功能.本文将介绍了DevExpress Reporting v18.2 的新功能,新版3 ...
- ecplise包的层次结构选择
ecplise包的层次结构选择 平坦方式: 分层方式:
- CentOS6.5 - yum对Mysql的安装与配置
一.mysql的安装 1.查看是否安装mysql [root@localhost ~]# rpm -qa | grep mysql 如果有进行卸载(以下三种方式选一种即可): -.el6.x86_64 ...
- 常用的字符串函数-S
header('content-type:text/html;charset=utf-f'); /* $var=addslashes($_GET['username']);//转义表单提交内容中的引号 ...