Skip to content

Commit e8fa285

Browse files
committed
add suffix tree and some geo3d
1 parent 5b1a83a commit e8fa285

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Collections of some commonly used algorithms.
1313
+ [x] SA-IS
1414
+ [ ] DC3
1515
+ [ ] GSACA
16-
+ [ ] 后缀树
16+
+ [x] 后缀树
1717
+ [x] 后缀自动机
1818
+ [ ] 子序列自动机
1919
+ [ ] AC自动机

computational-geometry/geo3d.cc

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <cmath>
2+
#include <cstdio>
3+
4+
typedef double flt;
5+
6+
const flt eps = 1e-8;
7+
int sgn(flt x) {return x < -eps ? -1 : x > eps;}
8+
9+
struct Point {
10+
flt x, y, z;
11+
Point() {}
12+
Point(flt x, flt y, flt z): x(x), y(y), z(z) {}
13+
bool operator < (const Point &rhs) const {
14+
return x < rhs.x || (x == rhs.x && y < rhs.y) || (x == rhs.x && y == rhs.y && z < rhs.z);
15+
}
16+
Point operator + (const Point &rhs) const {
17+
return Point(x + rhs.x, y + rhs.y, z + rhs.z);
18+
}
19+
Point operator - (const Point &rhs) const {
20+
return Point(x - rhs.x, y - rhs.y, z - rhs.z);
21+
}
22+
Point operator * (const flt k) const {
23+
return Point(x * k, y * k, z * k);
24+
}
25+
Point operator / (const flt k) const {
26+
return Point(x / k, y / k, z / k);
27+
}
28+
flt dot(const Point &rhs) const {
29+
return x * rhs.x + y * rhs.y + z * rhs.z;
30+
}
31+
Point det(const Point &rhs) const {
32+
return Point(y * rhs.z - z * rhs.y, z * rhs.x - x * rhs.z, x * rhs.y - y * rhs.x);
33+
}
34+
flt sqrlen() const {
35+
return x * x + y * y + z * z;
36+
}
37+
flt len() const {
38+
return sqrt(sqrlen());
39+
}
40+
void read() {
41+
scanf("%lf%lf%lf", &x, &y, &z);
42+
}
43+
void print() {
44+
printf("%.10f %.10f %.10f\n", x, y, z);
45+
}
46+
} A, B, C, D;
47+
48+
// 判断点C是否在直线AB上
49+
bool dotsInline(const Point &A, const Point &B, const Point &C) {
50+
return sgn((B - A).det(C - A).len()) == 0;
51+
}
52+
53+
// 判断直线AB和CD是否平行
54+
bool is_parallel(const Point &A, const Point &B, const Point &C, const Point &D) {
55+
return sgn((B - A).det(D - C).len()) == 0;
56+
}
57+
58+
// 判断直线AB和CD的交点, 如果直线异面, 返回的是两直线的垂线在AB上的垂足
59+
// 需要保证AB和CD不平行.
60+
Point intersect(const Point &A, const Point &B, const Point &C, const Point &D) {
61+
Point p0 = (C - A).det(D - C), p1 = (B - A).det(D - C);
62+
return A + (B - A) * (p0.dot(p1) / p1.sqrlen());
63+
}

string-utility/suffix-tree.cc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ namespace SuffixTree {
2121
int pos;
2222
position() {}
2323
position(node* u, int pos): u(u), pos(pos) {}
24-
void go(int o) {u = u->go[o], pos -= u->len;}
25-
bool can(int o) {return pos > u->go[o]->len;}
24+
void go(int o) {u = u->go[o]; pos -= u->len;}
25+
bool can(int o) {return u->go[o] && pos > u->go[o]->len;}
2626
} last;
2727
int s[SIZE], n;
2828
node *new_node(int st, int len) {
@@ -63,4 +63,12 @@ namespace SuffixTree {
6363
else last.u = last.u->link ? last.u->link : rt;
6464
}
6565
}
66+
// count the number of distinct substrings
67+
int count() {
68+
int ret = 0;
69+
for (int i = 1; i < sz - pool; ++i) {
70+
ret += std::min(n - pool[i].st, pool[i].len);
71+
}
72+
return ret;
73+
}
6674
}

0 commit comments

Comments
 (0)