Heim > Artikel > Backend-Entwicklung > LCS算法&最大公共子串&最长公共子序列 PHP 实现 最长公共上升子序列 最长公共子序列c语言 最长公共递增子序
求两个字符串的最大公共子串&最长公共子序列
<code>输入: abcbdab bdcaba</code>
<code>4</code>
即
bdcaba
与abcbdab
的最大公共子串长度为 4
常规思路
枚举法,算出两个字符串的所有子序列,然后分别作比较,选出最大的一个子串
缺点:对于一个长度为 n 的字符串,子串个数有 2 的 n 次方个,然后在依次比较两个字符串的子串,效率过低
动态规划 LCS算法
以动态规划的思想来解这个题,我们用一个二位数组 $dp[][]
来存储各个字符串对应的状态,具体什么含义就不细说了,百度一下,你就知道,主要是用 PHP 实现一下
代码如下:
<code><span><span>function</span><span>lcs</span><span>(<span>$str1</span>, <span>$str2</span>)</span> {</span><span>// 存储生成的二维矩阵</span><span>$dp</span> = <span>array</span>(); <span>// 最大子串长度</span><span>$max</span> = <span>0</span>; <span>for</span> (<span>$i</span> = <span>0</span>; <span>$i</span> $str1); <span>$i</span>++) { <span>for</span> (<span>$j</span> = <span>0</span>; <span>$j</span> $str2); <span>$j</span>++) { <span>if</span> (<span>$str1</span>[<span>$i</span>] == <span>$str2</span>[<span>$j</span>]) { <span>$dp</span>[<span>$i</span>][<span>$j</span>] = <span>isset</span>(<span>$dp</span>[<span>$i</span>-<span>1</span>][<span>$j</span>-<span>1</span>]) ? <span>$dp</span>[<span>$i</span>-<span>1</span>][<span>$j</span>-<span>1</span>] + <span>1</span> : <span>1</span>; } <span>else</span> { <span>$dp</span>[<span>$i</span>-<span>1</span>][<span>$j</span>] = <span>isset</span>(<span>$dp</span>[<span>$i</span>-<span>1</span>][<span>$j</span>]) ? <span>$dp</span>[<span>$i</span>-<span>1</span>][<span>$j</span>] : <span>0</span>; <span>$dp</span>[<span>$i</span>][<span>$j</span>-<span>1</span>] = <span>isset</span>(<span>$dp</span>[<span>$i</span>][<span>$j</span>-<span>1</span>]) ? <span>$dp</span>[<span>$i</span>][<span>$j</span>-<span>1</span>] : <span>0</span>; <span>$dp</span>[<span>$i</span>][<span>$j</span>] = <span>$dp</span>[<span>$i</span>-<span>1</span>][<span>$j</span>] > <span>$dp</span>[<span>$i</span>][<span>$j</span>-<span>1</span>] ? <span>$dp</span>[<span>$i</span>-<span>1</span>][<span>$j</span>] : <span>$dp</span>[<span>$i</span>][<span>$j</span>-<span>1</span>]; } <span>$max</span> = <span>$dp</span>[<span>$i</span>][<span>$j</span>] > <span>$max</span> ? <span>$dp</span>[<span>$i</span>][<span>$j</span>] : <span>$max</span>; } } <span>for</span> (<span>$i</span> = <span>0</span>; <span>$i</span> $str1); <span>$i</span>++) { <span>for</span> (<span>$j</span> = <span>0</span>; <span>$j</span> $str2); <span>$j</span>++) { <span>echo</span><span>$dp</span>[<span>$i</span>][<span>$j</span>] . <span>" "</span>; } <span>echo</span><span>"<br>"</span>; } var_dump(<span>$max</span>); } lcs(<span>"abcbdab"</span>, <span>"bdcaba"</span>);</code>
对应输出:
<code><span>0</span><span>0</span><span>0</span><span>1</span><span>1</span><span>1</span><span>1</span><span>1</span><span>1</span><span>1</span><span>2</span><span>2</span><span>1</span><span>1</span><span>2</span><span>2</span><span>2</span><span>2</span><span>1</span><span>1</span><span>2</span><span>2</span><span>3</span><span>3</span><span>1</span><span>2</span><span>2</span><span>2</span><span>3</span><span>3</span><span>1</span><span>2</span><span>2</span><span>3</span><span>3</span><span>4</span><span>1</span><span>2</span><span>2</span><span>3</span><span>4</span><span>4</span><span>int</span><span>4</span></code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });结论:通过动态规划,我们使时间复杂度降为了 O(nm),但是这样依旧有空间的浪费,有些数据的存储是不必要的,可以进一步做优化
以上就介绍了LCS算法&最大公共子串&最长公共子序列 PHP 实现,包括了最长公共子序列,php方面的内容,希望对PHP教程有兴趣的朋友有所帮助。