<table cellspacing="0" cellpadding="0"><tr><td class="t_f" id="postmessage_35120"> 参考 : http://www.cnblogs.com/ymind/archive/2012/03/27/fast-memory-efficient-Levenshtein-algorithm.html <br> <div class="blockcode"> <div id="code_YR5"><ol> <li>function _levenshtein($src, $dst){</li> <li> if (empty($src)) {</li> <li> return $dst;</li> <li> }</li> <li> if (empty($dst)) {</li> <li> return $src;</li> <li> }</li> <li> <li> $temp = array();</li> <li> <li> for($i = 0; $i <= strlen($src); $i ) {</li> <li> $temp[$i][0] = $i;</li> <li> }</li> <li> for($j = 0; $j <= strlen($dst); $j ) {</li> <li> $temp[0][$j] = $j;</li> <li> }</li> <li> for ($i = 1;$i <= strlen($src); $i ) {</li> <li> <li> $src_i = $src{$i - 1};</li> <li> <li> for ($j = 1; $j <= strlen($dst); $j ) {</li> <li> <li> $dst_j = $dst{$j - 1};</li> <li> <li> if ($src_i == $dst_j) {</li> <li> $cost = 0;</li> <li> } else {</li> <li> $cost = 1;</li> <li> }</li> <li> $temp[$i][$j] = min($temp[$i-1][$j] 1, $temp[$i][$j-1] 1, $temp[$i-1][$j-1] $cost);</li> <li> }</li> <li> <li> }</li> <li> return $temp[$i-1][$j-1];</li> <li>}</li> <li> <li>echo _levenshtein("hello", "HElloo");</li> </ol></div> <em onclick="copycode($('code_YR5'));">复制代码</em> </div> </td></tr></table> <div id="comment_35120" class="cm"> </div> <div id="post_rate_div_35120"></div> <br><br>