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 ...
随机推荐
- javascript高级程序设计第3版——第6章 面向对象的程序设计
第六章——面向对象的程序设计 这一章主要讲述了:面向对象的语言由于没有类/接口情况下工作的几种模式以及面向对象语言的继承: 模式:工厂模式,构造函数模式,原型模式 继承:原型式继承,寄生式继承,以及寄 ...
- css 积累
1.input 初始化 input { -webkit-tap-highlight-color: rgba(0,0,0,0); border: none; } input:focus { outlin ...
- javascript高级
数组及操作方法 数组就是一组数据的集合,javascript中,数组里面的数据可以是不同类型的. 定义数组的方法 //对象的实例创建 var aList = new Array(1,2,3); //直 ...
- mysql权限参考
mysql日常管理和应用工作中,大家经常会涉及到授权问题,下面,我们就列举下权限相关的参考. 1.管理权限(Administrative Privileges) Privilege Name ...
- 关于ajax的跨域
在前端开发中,跨域是经常遇到的问题,也是面试最喜欢问的问题,究其根本原因,是浏览器的同源策略所致,是浏览器为了避免不同域名不能共享cookie以及locationstorage等等,发起请求的时候无法 ...
- VS2017提醒找不到MSVCR110D.dll
我的电脑时win10我已解决,不能传文件,需要联系我
- Android View的滑动
Android View的滑动 文章目录 Android View的滑动 一.实现移动 1.1 layout() 1.2 设置位置偏移量 1.3 改变布局参数 1.4 动画 1.5 ScrollTo以 ...
- mysql Navicat客户端
Navicat是一个用来操作多种数据库的客户端. 可应用操作系统:Windows.macOS.Linux. 可应用 Navicat 产品:Navicat for MySQL.Navicat for P ...
- MapReduce实现Apriori算法
Apiroi算法在Hadoop MapReduce上的实现 输入格式: 一行为一个Bucket 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 34 36 38 ...
- 使用spring:aop中修改增强方法中的参数
大家都知道,在增强方法中,使用jp.getArgs()[index]可以获取传进来的参数,但是参数传进来之后,怎么改变它的值呢? 因为jp.getArgs()[index]获取到的只是数据的备份,所以 ...