Skip to content

Commit

Permalink
edit ch6+7 code
Browse files Browse the repository at this point in the history
  • Loading branch information
U-NUSSTF\dcssh authored and U-NUSSTF\dcssh committed Jun 30, 2020
1 parent 0386545 commit d655ea6
Show file tree
Hide file tree
Showing 8 changed files with 819 additions and 10 deletions.
4 changes: 2 additions & 2 deletions ch6/sa_lcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class SuffixArray {
for (int i = 0, L = 0; i < n; ++i) { // compute PLCP in O(n)
if (Phi[i] == -1) { PLCP[i] = 0; continue; } // special case
while ((i+L < n) && (Phi[i]+L < n) && (T[i+L] == T[Phi[i]+L]))
L++; // L incr max n times
++L; // L incr max n times
PLCP[i] = L;
L = max(L-1, 0); // L dec max n times
}
Expand Down Expand Up @@ -114,7 +114,7 @@ class SuffixArray {
}
};

const int MAX_N = 200010; // can go up to 400K chars
const int MAX_N = 450010; // can go up to 450K chars

char T[MAX_N];
char P[MAX_N];
Expand Down
5 changes: 3 additions & 2 deletions ch6/sa_lcp_slow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ int main() {
// analysis of this sort below: O(n log n) * cmp: O(n) = O(n^2 log n)
sort(SA.begin(), SA.end(), [](int a, int b) { // O(n^2 log n)
return strcmp(T+a, T+b) < 0;
});
}); // continued below

vi LCP(n);
LCP[0] = 0; // default value
for (int i = 1; i < n; ++i) { // compute by def, O(n^2)
int L = 0; // always reset L to 0
while (T[SA[i]+L] == T[SA[i-1]+L]) ++L; // same L-th char, ++L
while ((SA[i]+L < n) && (SA[i-1]+L < n) &&
(T[SA[i]+L] == T[SA[i-1]+L])) ++L; // same L-th char, ++L
LCP[i] = L;
}

Expand Down
5 changes: 3 additions & 2 deletions ch6/trie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ struct vertex {
vertex(char a): alphabet(a), exist(false) { child.assign(26, NULL); }
};

class Trie {
private:
class Trie { // this is TRIE
private: // NOT Suffix Trie
vertex* root;
public:
Trie() { root = new vertex('!'); }
Expand Down Expand Up @@ -59,5 +59,6 @@ int main() {
printf("'DOG' exist? %d\n", T.search("DOG")); // 0 (false)
printf("Starts with 'CA' exist? %d\n", T.startsWith("CA")); // 1 (true)
printf("Starts with 'Z' exist? %d\n", T.startsWith("Z")); // 0 (false)
printf("Starts with 'AT' exist? %d\n", T.startsWith("AT")); // 0 (false) for this Trie, but in a Suffix Trie, we have a suffix "AT" (from "CAT" or "RAT") that starts with "AT"
return 0;
}
19 changes: 16 additions & 3 deletions ch7/points_lines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,24 @@ bool collinear(point p, point q, point r) {
}

int main() {
vector<point> P;
P.emplace_back(2e-9, 0); // largest
P.push_back({0, 2}); // smallest
P.push_back({1e-9, 1}); // second smallest
sort(P.begin(), P.end());
for (auto &pt : P) // the result is
printf("%.9lf, %.9lf\n", pt.x, pt.y); // unexpected
// change
// const double EPS = 1e-9;
// to
// const double EPS = 1e-10;
// to fix that issue, Rule of Thumb: check the required precision

point P1, P2, P3(0, 1); // note that both P1 and P2 are (0.00, 0.00)
printf("%d\n", P1 == P2); // true
printf("%d\n", P1 == P3); // false
printf("%d\n", P1 == P2); // true
printf("%d\n", P1 == P3); // false

vector<point> P;
P.clear();
P.push_back(point(2, 2));
P.push_back(point(4, 3));
P.push_back(point(2, 4));
Expand Down
Loading

0 comments on commit d655ea6

Please sign in to comment.