diff --git a/algorithms/graph/kruskal.cpp b/algorithms/graph/kruskal.cpp index 87c4f68092877111e69835b0025206e3513a1546..96580a56ac0cdb18a2356c90760274cec0214658 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 241b64ade2a3f043574adce820adb26c2715491d..23581c22c7ed4c7bf164e092eb9da8dd818a9e09 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 5ae64d7e8dfb4a344d4ae4e6745605b70e48d75d..7ef42b0cd743ae2e17b1c0173be268c6f348a8b5 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)