时间:2021-07-01 10:21:17 帮助过:3人阅读
代码如下:
<?php
$a = 'abceee12345309878';
$b = 'abceeew2345i09878fsfsfsfabceeewsfsdfsfsabceeew';
$c = array();
$lenht1 = strlen($a);
$lenth2 = strlen($b);
$startTime = microtime(true);
for ($i=0;$i<$lenht1;$i++) {
for ($j=0;$j<$lenth2;$j++) {
$n = ($i-1>=0 && $j-1>=0)?$c[$i-1][$j-1]:0;
$n = ($a[$i] == $b[$j]) ? $n+1:0;
$c[$i][$j] = $n;
}
}
foreach ($c as $key=>$val) {
$max = max($val);
foreach ($val as $key1 =>$val1) {
if ($val1 == $max && $max>0) {
$cdStr[$max] = substr($b,$key1-$max+1,$max);
}
}
}
ksort($cdStr);
$endTime = microtime(true);
echo "Totle time is " . ($endTime - $startTime) . " s"."<br/>";
print_r(end($cdStr));
exit;
?>运行结果:
Totle time is 0.0012800693512 s abceee
相信看了这些案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!
相关阅读:
php自定义函数生成笛卡尔积的方法
PHP如何解决网站大流量与高并发
javascript数据类型和git使用代码详解
以上就是PHP怎么做出两个字符串的最长公共子串?的详细内容,更多请关注Gxl网其它相关文章!