Skip to content

Commit

Permalink
fix bugs and modified aho-corasick
Browse files Browse the repository at this point in the history
  • Loading branch information
NyaanNyaan committed Aug 7, 2024
1 parent d091038 commit 92e55cb
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 9 deletions.
25 changes: 17 additions & 8 deletions string/aho-corasick.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

#include "trie.hpp"

template <size_t X = 26, char margin = 'a'>
template <size_t X = 26, char margin = 'a', bool heavy = false>
struct AhoCorasick : Trie<X + 1, margin> {
using TRIE = Trie<X + 1, margin>;
using TRIE::next;
using TRIE::st;
using TRIE::TRIE;
vector<int> cnt;

void build(int heavy = true) {
void build() {
int n = st.size();
cnt.resize(n);
for (int i = 0; i < n; i++) {
Expand Down Expand Up @@ -55,15 +55,24 @@ struct AhoCorasick : Trie<X + 1, margin> {
}
}

vector<int> match(string s, int heavy = true) {
vector<int> res(heavy ? TRIE::size() : 1);
// heavy
// true : 各パターン文字列に対してマッチした回数を計算
// false : 全てのパターン文字列にマッチした回数の総和
conditional_t<heavy, unordered_map<int, long long>, long long> match(
string s) {
unordered_map<int, int> pos_cnt;
int pos = 0;
for (auto &c : s) {
pos = next(pos, c - margin);
if (heavy)
for (auto &x : st[pos].idxs) res[x]++;
else
res[0] += cnt[pos];
pos_cnt[pos]++;
}
conditional_t<heavy, unordered_map<int, long long>, long long> res{};
for (auto &[key, val] : pos_cnt) {
if constexpr (heavy) {
for (auto &x : st[key].idxs) res[x] += val;
} else {
res += 1LL * cnt[key] * val;
}
}
return res;
}
Expand Down
27 changes: 27 additions & 0 deletions verify/verify-yuki/yuki-0430-2.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#define PROBLEM "https://yukicoder.me/problems/no/430"
//
#include "../../template/template.hpp"
//
#include "../../string/aho-corasick.hpp"
using namespace Nyaan;

void q() {
ins(S);
ini(M);
AhoCorasick<26, 'A', true> ac;
rep(i, M) {
ins(C);
ac.add(C, i);
}
ac.build();
auto ans = ac.match(S);
ll bns = 0;
each2(key, val, ans) bns += val;
out(bns);
}

void Nyaan::solve() {
int t = 1;
// in(t);
while (t--) q();
}
24 changes: 24 additions & 0 deletions verify/verify-yuki/yuki-0430.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#define PROBLEM "https://yukicoder.me/problems/no/430"
//
#include "../../template/template.hpp"
//
#include "../../string/aho-corasick.hpp"
using namespace Nyaan;

void q() {
ins(S);
ini(M);
AhoCorasick<26, 'A', false> ac;
rep(i, M) {
ins(C);
ac.add(C, 0);
}
ac.build();
out(ac.match(S));
}

void Nyaan::solve() {
int t = 1;
// in(t);
while (t--) q();
}
2 changes: 1 addition & 1 deletion verify/verify-yuki/yuki-1269.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void Nyaan::solve() {

AhoCorasick<10, '0'> aho;
rep(i, sz(ss)) aho.add(ss[i], i);
aho.build(false);
aho.build();

vm dp(aho.size());
dp[0] = 1;
Expand Down

0 comments on commit 92e55cb

Please sign in to comment.