最大匹配字符串LCS,The Longest Common Substring
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的更多相关文章
- 最长公共子串(LCS:Longest Common Substring)
最长公共子串(LCS:Longest Common Substring)是一个非常经典的面试题目,本人在乐视二面中被面试官问过,惨败在该题目中. 什么是最长公共子串 最长公共子串问题的基本表述为:给定 ...
- 最长公共子串LCS(Longest Common Substring)
一.问题描述 寻求两个字符串中的最大公共字串,其中子串是指字符串中连续的字符组成的,而不是像子序列,按照字符的前后顺序组成.如str1="sgabacbadfgbacst",str ...
- SPOJ 1811. Longest Common Substring (LCS,两个字符串的最长公共子串, 后缀自动机SAM)
1811. Longest Common Substring Problem code: LCS A string is finite sequence of characters over a no ...
- 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, \(\ ...
- spoj 1811 LCS - Longest Common Substring (后缀自己主动机)
spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...
- SPOJ1812 LCS2 - Longest Common Substring II【SAM LCS】
LCS2 - Longest Common Substring II 多个字符串找最长公共子串 以其中一个串建\(SAM\),然后用其他串一个个去匹配,每次的匹配方式和两个串找\(LCS\)一样,就是 ...
- spoj1811 LCS - Longest Common Substring
地址:http://www.spoj.com/problems/LCS/ 题面: LCS - Longest Common Substring no tags A string is finite ...
- 后缀自动机(SAM) :SPOJ LCS - Longest Common Substring
LCS - Longest Common Substring no tags A string is finite sequence of characters over a non-empty f ...
- 【SP1811】LCS - Longest Common Substring
[SP1811]LCS - Longest Common Substring 题面 洛谷 题解 建好后缀自动机后从初始状态沿着现在的边匹配, 如果失配则跳它的后缀链接,因为你跳后缀链接到达的\(End ...
随机推荐
- 编程中的链式调用:Scala示例
编程中的链式调用与Linux Shell 中的管道类似.Linux Shell 中的管道 ,会将管道连接的上一个程序的结果, 传递给管道连接的下一个程序作为参数进行处理,依次串联起N个实用程序形成流水 ...
- IDEA相关知识整理
一.离线下载插件[也可以通过代理的方式下载插件] http://plugins.jetbrains.com/ 下载离线插件 settings -> plugins -> install p ...
- pyspider操作千万级库,pyspider在对接量级较大库的策略
pyspider操作千万级库,pyspider在对接量级较大库的策略 如果是需要pyspider正常的流程去执行,那必然是会在on_strat()时任务执行超时,可能只读取出几万条或十几万条数据就会被 ...
- H.264流媒体协议格式中的Annex B格式和AVCC格式深度解析
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/Romantic_Energy/article/details/50508332本文需要读者对H.26 ...
- kali linux 压缩文件解压缩命令(包含7z)
tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) ——————————————— .gz 解压1 ...
- css相关知识点
一.CSS的引入方式 1.1 css的介绍 HTML:超文本标记语言.从语义的角度描述页面结构. CSS:层叠样式表.从审美的角度负责页面样式. JS:JavaScript .从交互的角度描述页面行为 ...
- MyEclipse配置默认自带的Java代码格式化
1.配置注释一大段代码块后,格式化代码,已经注释的代码仍然保持原样 (1)首先,Window -> Prefrences -> Java -> Code Style -> Fo ...
- 翻唱 - shape of you - 个个版本
翻唱: http://7j1xky.com1.z0.glb.clouddn.com/1525514286196.mp4 乐队版-我的翻唱-混合 http://7j1xky.com1.z0.glb.cl ...
- STM32开发 -- 4G模块开发详解(转)
STM32开发 -- 4G模块开发详解(1) STM32开发 -- 4G模块开发详解(2) STM32开发 -- 4G模块开发详解(3) STM32开发 -- 4G模块开发详解(4)
- Eloquent JavaScript #06# class
索引 Notes this Prototype 类 class符号 覆盖派生属性 Maps Symbols iterator接口 Getters, setters, and statics 继承 in ...