1
0
Fork 0
cp-templates/string/ac-automaton.cc

89 lines
2.1 KiB
C++
Raw Normal View History

2024-01-19 10:22:04 +00:00
// lg-5357.cc
2024-01-19 10:21:06 +00:00
#include "../include.hh"
void solve(vector<string> ss, string t) {
int n = ss.size();
vector<vector<int>> trie(1, vector<int>(26));
vector<int> tag(1);
vector<int> id(1);
vector<int> alias(n, -1);
int last = 0;
// build trie for ss
for (int i = 0; i < n; ++i) {
auto&& s = ss[i];
int curr = 0;
for (auto&& c : s) {
int x = c - 'a';
if (!trie[curr][x])
trie[curr][x] = ++last,
tag.push_back({}),
id.push_back({}),
trie.push_back(vector<int>(26));
curr = trie[curr][x];
}
if (!tag[curr]) id[curr] = i;
else alias[i] = id[curr];
tag[curr] = 1;
}
// build fail
vector<int> fail(last + 1);
vector<vector<int>> suc(last + 1);
2024-01-19 10:24:40 +00:00
deque<int> dq;
2024-01-19 10:21:06 +00:00
for (int i = 0; i < 26; ++i) {
2024-01-19 10:24:40 +00:00
if (trie[0][i]) dq.push_back(trie[0][i]), suc[0].push_back(trie[0][i]);
2024-01-19 10:21:06 +00:00
}
while (dq.size()) {
2024-01-19 10:24:40 +00:00
int c = dq.front(); dq.pop_front();
2024-01-19 10:21:06 +00:00
for (int i = 0; i < 26; ++i) {
if (trie[c][i]) {
fail[trie[c][i]] = trie[fail[c]][i];
suc[trie[fail[c]][i]].push_back(trie[c][i]);
2024-01-19 10:24:40 +00:00
dq.push_back(trie[c][i]);
2024-01-19 10:21:06 +00:00
} else {
trie[c][i] = trie[fail[c]][i];
}
}
}
// match t
vector<int> oc(last + 1);
int curr = 0;
for (auto&& c : t) {
int x = c - 'a';
2024-01-19 10:37:56 +00:00
// if (!trie[curr][x]) curr = fail[curr];
2024-01-19 10:21:06 +00:00
curr = trie[curr][x];
oc[curr] += 1;
}
// collect result
vector<int> res(n);
auto dfs = [&] (auto dfs, int cur) -> int {
int cnt = oc[cur];
for (auto&& c : suc[cur]) {
cnt += dfs(dfs, c);
}
if (cnt && tag[cur]) {
res[id[cur]] += cnt;
}
return cnt;
};
dfs(dfs, 0);
for (int i = 0; i < n; ++i) {
if (alias[i] != -1) cout << res[alias[i]];
else cout << res[i];
cout << endl;
}
}
int main() {
read(int, n);
readvec(string, ss, n);
read(string, t);
solve(ss, t);
}