此代码中存在致命错误:
第108行已删除了呼叫时间传递引用。这是 Boyer Moore算法
function suffixes($pattern, &$suffixes)
{
$m = strlen($pattern);
$suffixes[$m - 1] = $m;
$g = $m - 1;
for ($i = $m - 2; $i >= 0; --$i) {
if ($i > $g && $suffixes[$i + $m - 1 - $f] < $i - $g) {
$suffixes[$i] = $suffixes[$i + $m - 1 - $f];
} else {
if ($i < $g) {
$g = $i;
}
$f = $i;
while ($g >= 0 && $pattern[$g] == $pattern[$g + $m - 1 - $f]) {
$g--;
}
$suffixes[$i] = $f - $g;
}
}
}
功能badCharacters
function badCharacters($pattern, &$badChars)
{
$m = strlen($pattern);
for ($i = 0; $i < $m - 1; ++$i) {
$badChars[$pattern{$i}] = $m - $i - 1;
}
}
功能goodSuffixes
function goodSuffixes($pattern, &$goodSuffixes)
{
$m = strlen($pattern);
$suff = array();
suffixes($pattern, $suff);
for ($i = 0; $i < $m; $i++) {
$goodSuffixes[$i] = $m;
}
for ($i = $m - 1; $i >= 0; $i--) {
if ($suff[$i] == $i + 1) {
for ($j = 0; $j < $m - $i - 1; $j++) {
if ($goodSuffixes[$j] == $m) {
$goodSuffixes[$j] = $m - $i - 1;
}
}
}
}
for ($i = 0; $i < $m - 2; $i++) {
$goodSuffixes[$m - 1 - $suff[$i]] = $m - $i - 1;
}
}
功能boyer_moore
function boyer_moore($pattern, $text)
{
$n = strlen($text);
$m = strlen($pattern);
$goodSuffixes = array();
$badCharacters = array();
//this is the line 108
goodSuffixes($pattern, &$goodSuffixes);
badCharacters($pattern, &$badCharacters);
$j = 0;
while ($j < $n - $m) {
for ($i = $m - 1; $i >= 0 && $pattern[$i] == $text[$i + $j]; $i--);
if ($i < 0) {
echo $j;
$j += $goodSuffixes[0];
} else {
$j += max($goodSuffixes[$i], $badCharacters[$text[$i + $j]] - $m + $i + 1);
}
}
}
boyer_moore($pattern, $text);
导致此错误的原因是什么?
答案 0 :(得分:0)
从错误消息中可以清楚地看出:
goodSuffixes($pattern, &$goodSuffixes);
badCharacters($pattern, &$badCharacters);
'&amp;'是在调用时尝试通过引用传递的字符。删除'&amp;'从2个变量名前面。
goodSuffixes($pattern, $goodSuffixes);
badCharacters($pattern, $badCharacters);