From 43eb105f8539a5c07cdb1322a2b42d538e51b16b Mon Sep 17 00:00:00 2001 From: Bruno Freitas Tissei <bft15@inf.ufpr.br> Date: Fri, 22 Nov 2019 01:16:20 -0300 Subject: [PATCH] Small fixes Signed-off-by: Bruno Freitas Tissei <bft15@inf.ufpr.br> --- algorithms/graph/kruskal.cpp | 8 +++---- algorithms/string/kmp.cpp | 31 ++++++++++++--------------- algorithms/structure/segment_tree.cpp | 2 +- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/algorithms/graph/kruskal.cpp b/algorithms/graph/kruskal.cpp index 87c4f68..96580a5 100644 --- a/algorithms/graph/kruskal.cpp +++ b/algorithms/graph/kruskal.cpp @@ -6,8 +6,8 @@ /// Include: /// - structure/disjoint_set -typedef pair<ii,int> iii; -vector<iii> edges; +using edge = pair<ii,int>; +vector<edge> edges; struct Kruskal { int N; @@ -17,8 +17,8 @@ struct Kruskal { // Minimum Spanning Tree: comp = less<int>() // Maximum Spanning Tree: comp = greater<int>() - int run(vector<iii> &mst, function<bool(int,int)> comp) { - sort(all(edges), [&](const iii &a, const iii &b) { + int run(vector<edge> &mst, function<bool(int,int)> comp) { + sort(all(edges), [&](const edge &a, const edge &b) { return comp(a.se, b.se); }); diff --git a/algorithms/string/kmp.cpp b/algorithms/string/kmp.cpp index 241b64a..23581c2 100644 --- a/algorithms/string/kmp.cpp +++ b/algorithms/string/kmp.cpp @@ -9,36 +9,33 @@ struct KMP { string patt; - vector<int> table; + vector<int> pi; KMP(string patt) : - patt(patt), table(patt.size()+1) + patt(patt), pi(patt.size()) { preprocess(); } void preprocess() { - fill(all(table), -1); + patt[0] = 0; + for (int i = 1, j = 0; i < patt.size(); ++i) { + while (j > 0 && patt[i] != patt[j]) + j = pi[j - 1]; - for (int i = 0, j = -1; i < patt.size(); ++i) { - while (j >= 0 && patt[i] != patt[j]) - j = table[j]; - table[i + 1] = ++j; + if (patt[i] == patt[j]) j++; + pi[i] = j; } } - vector<int> search(const string &txt) { - vector<int> occurs; - + void search(const string &txt) { for (int i = 0, j = 0; i < txt.size(); ++i) { - while (j >= 0 && txt[i] != patt[j]) - j = table[j]; - j++; + while (j > 0 && txt[i] != patt[j]) + j = pi[j - 1]; + if (txt[i] == patt[j]) j++; if (j == patt.size()) { - occurs.pb(i - j); - j = table[j]; + cout << "Pattern found at " << (i - j) << ende; + j = pi[j - 1]; } } - - return occurs; } }; diff --git a/algorithms/structure/segment_tree.cpp b/algorithms/structure/segment_tree.cpp index 5ae64d7..7ef42b0 100644 --- a/algorithms/structure/segment_tree.cpp +++ b/algorithms/structure/segment_tree.cpp @@ -20,7 +20,7 @@ struct SegmentTree { T ident = T(); vector<T> tree; - SegmentTree(F &func) : func(func), tree(MAX*4) {} + SegmentTree(F func) : func(func), tree(MAX*4) {} void build(const vector<T> &v, int node = 1, int l = 0, int r = N - 1) -- GitLab