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. 【Redis学习之六】Redis数据类型:集合和有序集合

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 redis-2.8.18 一.集合 Set无序的.去重的元素 ...

  2. Linux基础命令---tail显示文本

    tail 显示文本文件尾部的部分内容,默认显示最后10行. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. 1.语法         ...

  3. The Little Prince-12/06

    The Little Prince-12/06 “That doesn't matter. Draw me a sheep.” When the prince ask the planet to dr ...

  4. 事务 c#

    事务->:事务是恢复和并发控制的基本单位 ->事务具有四个特性:原子性.隔离性.一致性.持久性.这四个特性通常称为ACID Begin transaction/tran   --开始事务 ...

  5. QT开发基础教程

    http://www.qter.org/portal.php?mod=view&aid=11

  6. Ubuntu16.04+cuda8.0rc+opencv3.1.0+caffe+Theano+torch7搭建教程

    https://blog.csdn.net/jywowaa/article/details/52263711 学习中用到深度学习的框架,需要搭建caffe.theano和torch框架.经过一个月的不 ...

  7. linux 函数库使用

    程序函数库可分为3种类型:静态函 数库(static libraries).共享函数库(shared libraries)和动态加载函数库(dynamically loaded libraries) ...

  8. 关于__declspec(dllexport)

    windows下dll动态库函数的导入与导出. __declspec Microsoft Specific __declspec ( extended-attribute ) declarator l ...

  9. VMware Ubuntu 虚拟机安装 VMwareTools (VMware虚拟机如何与主机互相复制文件)

    1.关闭虚拟机 2.CD-ROM开机连接取消对号 3.开启虚拟机 4.此时可能提示安装,点击即可 或者在VMware上方选择 :虚拟机 → 安装VMware Tools 5.虚拟机桌面会弹出相应安装包 ...

  10. Codeforces 40E Number Table - 组合数学

    题目传送门 传送门I 传送门II 题目大意 给定一个$n\times m$的网格,每个格子上要么填$1$,要么填$-1$,有$k$个位置上的数是已经填好的,其他位置都是空的.问有多少种填法使得任意一行 ...