Skip to content

Commit

Permalink
Merge pull request #82 from BRUTEUdesc/joao
Browse files Browse the repository at this point in the history
fix aho corasick
  • Loading branch information
joaomarcosth9 authored Mar 12, 2024
2 parents 15a6d3b + ee9dab2 commit 02a2f7d
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ template <ll MINL = (ll)-1e9 - 5, ll MAXR = (ll)1e9 + 5> struct SegTree {
return Rc[u];
}

SegTree() {
newnode();
}
SegTree() { newnode(); }

ll query(int u, ll l, ll r, ll L, ll R) {
if (l > R || r < L) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ template <ll MINL = (ll)-1e9 - 5, ll MAXR = (ll)1e9 + 5> struct SegTree {
return Rc[u];
}

SegTree() {
newnode();
}
SegTree() { newnode(); }

void push(int u, ll l, ll r) {
if (replace[u]) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
template <ll MINL = (ll)-1e9 - 5, ll MAXR = (ll)1e9 + 5> struct SegTree {
ll merge(ll a, ll b) { return a + b; }
const ll neutral = 0;

vector<ll> t;
vector<int> Lc, Rc, roots;

inline int newnode() {
t.push_back(neutral);
Lc.push_back(-1);
Rc.push_back(-1);
return (int)t.size() - 1;
}

inline int le(int u) {
if (Lc[u] == -1) {
Lc[u] = newnode();
}
return Lc[u];
}

inline int ri(int u) {
if (Rc[u] == -1) {
Rc[u] = newnode();
}
return Rc[u];
}

SegTree() {
roots.push_back(newnode());
}


SegTree() { roots.push_back(newnode()); }

ll query(int u, ll l, ll r, ll L, ll R) {
if (l > R || r < L) {
return neutral;
Expand All @@ -43,11 +41,12 @@ template <ll MINL = (ll)-1e9 - 5, ll MAXR = (ll)1e9 + 5> struct SegTree {
return merge(ql, qr);
}
ll query(ll l, ll r, int root = -1) {
if (root == -1) root = roots.back();
if (root == -1)
root = roots.back();
debug(root, MINL, MAXR, l, r);
return query(root, MINL, MAXR, l, r);
}

void update(int u, int old, ll l, ll r, ll i, ll x) {
if (l == r) {
t[u] = x; // substitui
Expand All @@ -66,7 +65,8 @@ template <ll MINL = (ll)-1e9 - 5, ll MAXR = (ll)1e9 + 5> struct SegTree {
}
int update(ll i, ll x, int root = -1) {
int new_root = newnode();
if (root == -1) root = roots.back();
if (root == -1)
root = roots.back();
update(new_root, root, MINL, MAXR, i, x);
roots.push_back(new_root);
return roots.back();
Expand Down
12 changes: 7 additions & 5 deletions Codigos/Grafos/HLD/HLD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ struct HLD {
int n, t;
vector<vector<int>> adj;
vector<int> sz, pos, par, head, who;
bool e = 0;
SegTree seg;
bool e = 0; // flag pra dizer se eh de aresta ou nao
SegTree seg; // pode usar qualquer estrutura de dados aqui

void dfs_sz(int u, int p = -1) {
sz[u] = 1;
Expand All @@ -18,7 +18,8 @@ struct HLD {
}
}
void dfs_hld(int u, int p = -1) {
who[pos[u] = t++] = u;
who[t] = u;
pos[u] = t++;
for (int v : adj[u]) {
if (v != p) {
par[v] = u;
Expand Down Expand Up @@ -73,6 +74,7 @@ struct HLD {

ll query(int u, int v) {
if (e && u == v) {
#warning "Tratar esse caso"
return seg.neutral;
}
if (pos[u] > pos[v]) {
Expand All @@ -90,7 +92,7 @@ struct HLD {
if (e && sz[u] == 1) {
return seg.neutral;
}
return seg.query(pos[u], pos[u] + sz[u] - 1);
return seg.query(pos[u] + e, pos[u] + sz[u] - 1);
}

void update(int u, int v, ll k, bool replace = false) {
Expand All @@ -111,7 +113,7 @@ struct HLD {
if (e && sz[u] == 1) {
return;
}
seg.update(pos[u], pos[u] + sz[u] - 1, k, replace);
seg.update(pos[u] + e, pos[u] + sz[u] - 1, k, replace);
}

int lca(int u, int v) {
Expand Down
2 changes: 0 additions & 2 deletions Codigos/Grafos/Inverse-Graph/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Inverse Graph

<!-- DESCRIPTION -->
Algoritmo que encontra as componentes conexas quando se é dado o grafo complemento.
<!-- DESCRIPTION -->

Resolve problemas em que se deseja encontrar as componentes conexas quando são dadas as arestas que não pertencem ao grafo

Expand Down
5 changes: 4 additions & 1 deletion Codigos/String/Aho-Corasick/aho_corasick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct AC {

int get_link(int u) {
if (link[u] == -1) {
link[u] = par[u] ? go(get_link(par[u]), pch[u]) : 0;
link[u] = par[u] > 0 ? go(get_link(par[u]), pch[u]) : 0;
}
return link[u];
}
Expand All @@ -59,4 +59,7 @@ struct AC {
}
return out_link[u];
}

bool matched(int u) { return out[u] || exit(u) != 0; }

} aho;
2 changes: 0 additions & 2 deletions Codigos/String/Trie/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# [Trie](trie.cpp)

<!-- DESCRIPTION -->
Estrutura que guarda informações indexadas por palavra.
<!-- DESCRIPTION -->

Útil encontrar todos os prefixos inseridos anteriormente de uma palavra específica.

Expand Down
Binary file modified PDF/Almanaque.pdf
Binary file not shown.

0 comments on commit 02a2f7d

Please sign in to comment.