-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathac.php
103 lines (98 loc) · 2.11 KB
/
ac.php
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/**
* Created by PhpStorm.
* User: jianfenliu
* Date: 2019/1/20
* Time: 下午4:58
*/
//取任意数的组合
/**
* @param $arr input array
* @param $m input select num
*/
function combination($arr, $m){
$res = [];
$len = count($arr);
if($m <=0 || $m >$len)
return $res;
for($i = 0; $i < $len; $i++){
$t = [$arr[$i]];
if($m == 1){
$res[] = $t;
}else{
$b = array_slice($arr, $i+1);
$c = combination($b, $m-1);
foreach($c as $v){
$res[] = array_merge($t, $v);
}
}
}
return $res;
}
$s = combination(['a','b','c','d','e'], 4);
function arrangement($a, $m) {
$r = array();
$n = count($a);
if ($m <= 0 || $m > $n) {
return $r;
}
for ($i=0; $i<$n; $i++) {
$b = $a;
$t = array_splice($b, $i, 1);
if ($m == 1) {
$r[] = $t;
} else {
$c = arrangement($b, $m-1);
foreach ($c as $v) {
$r[] = array_merge($t, $v);
}
}
}
return $r;
}
//print_r(combination(['a','b','c'],2));
//https://www.cnblogs.com/ZuoAndFutureGirl/p/9028287.html
//next 数组中存储的是这个字符串前缀和后缀的中相同字符串的长度。
function kmpSearch($s, $p){
$next = getNextVal($p);
$sLen = count($s);$pLen = count($p);
while($i < $sLen && $j < $pLen){
if($j == -1 || $s[$i] == $p[$j]){
$i++;$j++;
}else{
$j = $next[$j];
}
}
if($j == $pLen)
return $i - $j;
else
return -1;
}
function getNextVal($p){
$pLen = count($p);
$next[0] = -1;
$k = -1; $j=0;
while(0 < $pLen-1){
if($k == -1 || $p[$j] == $[$k]){
$k++;
$j++;
if($p[$j] != $p[$k])
$next[$j] = $k;
else
$next[$j] = $next[$k];
}else{
$k = $next[$k];
}
}
}
//字典树
一次操作,
array
access O(1)
insert O (n)
delete o (n)
Linked List
head tail next
access o(n)
insert O(1)
delete O(1)