public enum BackTracking
{
UP,
LEFT,
NEITHER,
UP_AND_LEFT
} public abstract class LCSBaseMatch
{
/// <summary>
/// 设置连续字符的匹配值
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
protected virtual int ConsecutiveMeasure(int length)
{
return length * length;
} /// <summary>
/// 获取两个string字符串的匹配度
/// </summary>
/// <param name="list1"></param>
/// <param name="list2"></param>
/// <returns></returns>
public virtual int GetMatchScoreOfLCS(char[] list1, char[] list2)
{
int[,] lcs;//最大匹配度
BackTracking[,] backTracer;//需要执行的操作 int score = GetMatchScoreOfLCS(list1, list2, out lcs, out backTracer);//最终匹配度
return score;
} /// <summary>
/// 计算匹配度
/// </summary>
/// <param name="list1"></param>
/// <param name="list2"></param>
/// <param name="lcs"></param>
/// <param name="backTracer"></param>
/// <returns></returns>
protected int GetMatchScoreOfLCS(char[] list1, char[] list2, out int[,] lcs, out BackTracking[,] backTracer)
{
int m = list1.Length;
int n = list2.Length; lcs = new int[m, n];//最大匹配度
backTracer = new BackTracking[m, n];//需要执行的操作
int[,] w = new int[m, n];//连续匹配的长度
int i, j; #region 初始化lcs、backTracer
for (i = 0; i < m; ++i)
{
lcs[i, 0] = 0;
backTracer[i, 0] = BackTracking.UP;
}
for (j = 0; j < n; ++j)
{
lcs[0, j] = 0;
backTracer[0, j] = BackTracking.LEFT;
}
#endregion #region 给lcs、backTracer、w赋值
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
if (list1[i] == list2[j])
{
int k = 0;
int prev = 0;
if (i > 0 && j > 0)
{
k = w[i - 1, j - 1];
prev = lcs[i - 1, j - 1];
}
//eviation unit between k+1 and k instead of 1 in basic LCS
lcs[i, j] = prev + ConsecutiveMeasure(k + 1) - ConsecutiveMeasure(k);
backTracer[i, j] = BackTracking.UP_AND_LEFT;
w[i, j] = k + 1;
}
if (i > 0 && (lcs[i - 1, j] > lcs[i, j]))
{
lcs[i, j] = lcs[i - 1, j];
backTracer[i, j] = BackTracking.UP;
w[i, j] = 0;
}
if (j > 0 && (lcs[i, j - 1] > lcs[i, j]))
{
lcs[i, j] = lcs[i, j - 1];
backTracer[i, j] = BackTracking.LEFT;
w[i, j] = 0;
}
}
}
#endregion return lcs[m - 1, n - 1];//最终匹配度
} } public class LCSMatchForString : LCSBaseMatch
{
/// <summary>
/// get The Longest Common Substring
/// </summary>
/// <param name="list1"></param>
/// <param name="list2"></param>
/// <returns></returns>
public string LCS(string s1, string s2)
{
char[] list1 = s1.ToArray();
char[] list2 = s2.ToArray();
int m = list1.Length;
int n = list2.Length; int[,] lcs ;//最大匹配度
BackTracking[,] backTracer ;//需要执行的操作 int score = GetMatchScoreOfLCS(list1,list2,out lcs,out backTracer);//最终匹配度 #region 获取最大匹配的字符串
int i = m - 1;
int j = n - 1;
string subseq = "";
//trace the backtracking matrix.
while (i >=0 && j >=0)
{
if (backTracer[i, j] == BackTracking.NEITHER) break;
if (backTracer[i, j] == BackTracking.UP_AND_LEFT)
{
subseq = list1[i]+ subseq;
i--;
j--;
}
else if (backTracer[i, j] == BackTracking.UP)
{
i--;
}
else if (backTracer[i, j] == BackTracking.LEFT)
{
j--;
}
}
#endregion return subseq;
}
}

  

最大匹配字符串LCS,The Longest Common Substring的更多相关文章

  1. 最长公共子串(LCS:Longest Common Substring)

    最长公共子串(LCS:Longest Common Substring)是一个非常经典的面试题目,本人在乐视二面中被面试官问过,惨败在该题目中. 什么是最长公共子串 最长公共子串问题的基本表述为:给定 ...

  2. 最长公共子串LCS(Longest Common Substring)

    一.问题描述 寻求两个字符串中的最大公共字串,其中子串是指字符串中连续的字符组成的,而不是像子序列,按照字符的前后顺序组成.如str1="sgabacbadfgbacst",str ...

  3. SPOJ 1811. Longest Common Substring (LCS,两个字符串的最长公共子串, 后缀自动机SAM)

    1811. Longest Common Substring Problem code: LCS A string is finite sequence of characters over a no ...

  4. LCS2 - Longest Common Substring II(spoj1812)(sam(后缀自动机)+多串LCS)

    A string is finite sequence of characters over a non-empty finite set \(\sum\). In this problem, \(\ ...

  5. spoj 1811 LCS - Longest Common Substring (后缀自己主动机)

    spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...

  6. SPOJ1812 LCS2 - Longest Common Substring II【SAM LCS】

    LCS2 - Longest Common Substring II 多个字符串找最长公共子串 以其中一个串建\(SAM\),然后用其他串一个个去匹配,每次的匹配方式和两个串找\(LCS\)一样,就是 ...

  7. spoj1811 LCS - Longest Common Substring

    地址:http://www.spoj.com/problems/LCS/ 题面: LCS - Longest Common Substring no tags  A string is finite ...

  8. 后缀自动机(SAM) :SPOJ LCS - Longest Common Substring

    LCS - Longest Common Substring no tags  A string is finite sequence of characters over a non-empty f ...

  9. 【SP1811】LCS - Longest Common Substring

    [SP1811]LCS - Longest Common Substring 题面 洛谷 题解 建好后缀自动机后从初始状态沿着现在的边匹配, 如果失配则跳它的后缀链接,因为你跳后缀链接到达的\(End ...

随机推荐

  1. 基于FPGA摄像头图像采集显示系统

    本系统主要由FPGA主控模块.图像采集模块.图像存储模块以及图像显示模块等模块组成.其中图像采集模块选择OV7670摄像头模块,完成对视频图像的采集和解码功能,并以RGB565标准输出RGB 5:6: ...

  2. 【JavaScript 6连载】四、apply和call的用法

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  3. animate和translate

    transition, transform, tanslate,animation分别为过渡,变换,平移.动画.transform的属性包括:rotate() / skew() / scale() / ...

  4. mongoDB 的介绍

    一.常用的网站 MongoDB  --  2009年被发布 MongoDB的官网:  www.mongodb.org 可以下载安装包    和  使用文档 MongoDB国内官方网站:  www.mo ...

  5. Kattis之旅——Rational Arithmetic

    Input The first line of input contains one integer, giving the number of operations to perform. Then ...

  6. Kivy之常用的小知识

    1.设置标题 app.title = '测试' 2.设置屏幕长度 Window.size=1000,600 3.设置屏幕右上角icon app.title = r'C:\Users\Administr ...

  7. K8S学习笔记之Flannel解读

    0x00 概述 我们知道docker官方并没有提供多主机的容器通信方案,单机网络的模式主要有host,container,brige,none.none这种模式,顾名思义就是docker本身不去管理网 ...

  8. Java Thread.yield详解

    这是Java中的一种线程让步方法,让Java中的线程从执行状态变成就绪状态,然后处理器再从就绪队列中挑选线程进行执行(优先级大的,被挑选的概率较大),这种转换也不确定,让或者不让都是取决与处理器,线程 ...

  9. Golang两种方法实现MD5加密

    package main import ( "crypto/md5" "fmt" "io" ) func main() { str := & ...

  10. Mysql报错java.sql.SQLException:null,message from server:"Host '27,45,38,132' is not allowed to connect

    Mysql报错java.sql.SQLException:null,message from server:"Host '27,45,38,132' is not allowed to co ...