-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSF.php
More file actions
45 lines (43 loc) · 757 Bytes
/
Copy pathSF.php
File metadata and controls
45 lines (43 loc) · 757 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
/**
* KMP算法辅助函数
*/
function getNext($str)
{
$next =array();
$m = 0;
$next[0] = 0;
for ($i=1; $i < strlen($str); $i++) {
if($str[$i] == $str[$m])
{
$next[$i] = $next[$i-1] +1;
$m++;
}else{
$m = 0;
$next[$i] = $next[$m];
}
}
return $next;
}
//KMP查找算法
function KMP($str, $par)
{
$next = $this->getNext($par);
$str_len = strlen($str);
$par_len = strlen($par);
for ($i=0,$j=0 ; $i < $str_len AND $j < $par_len; ) {
if($str[$i] == $par[$j])
{
$i++;
$j++;
}else{
if($j == 0)
{
$i ++;
}
$j = $next[($j-1) >= 0 ? ($j-1) : 0];
}
}
if($j == strlen($par)) return $i-$j+1;
return false;
}