2022-09-29 12:46:22 +00:00
|
|
|
#include <iostream>
|
2022-12-31 09:15:29 +00:00
|
|
|
#include <memory>
|
2022-09-29 12:46:22 +00:00
|
|
|
#include <queue>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using std::cin;
|
|
|
|
using std::cout;
|
|
|
|
const char endl = '\n';
|
|
|
|
|
|
|
|
class AcAutomaton {
|
|
|
|
private:
|
|
|
|
struct node {
|
|
|
|
int cnt;
|
2022-12-31 09:15:29 +00:00
|
|
|
std::shared_ptr<node> child[26], fail;
|
2022-09-29 12:46:22 +00:00
|
|
|
|
|
|
|
node()
|
|
|
|
: cnt(0), fail(nullptr) {
|
2022-12-31 09:15:29 +00:00
|
|
|
std::fill(std::begin(child), std::end(child), nullptr);
|
2022-09-29 12:46:22 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-12-31 09:15:29 +00:00
|
|
|
std::shared_ptr<node> root;
|
2022-09-29 12:46:22 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
AcAutomaton()
|
|
|
|
: root(new node()) {}
|
|
|
|
|
|
|
|
void insert(std::string s) {
|
2022-12-31 09:15:29 +00:00
|
|
|
std::shared_ptr<node> cur = root;
|
2022-09-29 12:46:22 +00:00
|
|
|
|
|
|
|
for (char c : s) {
|
|
|
|
if (cur->child[c - 'a'] == nullptr) {
|
2022-12-31 09:15:29 +00:00
|
|
|
cur->child[c - 'a'] = std::make_shared<node>();
|
2022-09-29 12:46:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cur = cur->child[c - 'a'];
|
|
|
|
}
|
|
|
|
|
|
|
|
cur->cnt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void build() {
|
2022-12-31 09:15:29 +00:00
|
|
|
std::queue<std::shared_ptr<node>> q;
|
2022-09-29 12:46:22 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < 26; i++) {
|
|
|
|
if (root->child[i] != nullptr) {
|
2022-12-31 09:15:29 +00:00
|
|
|
q.emplace(root->child[i]);
|
2022-09-29 12:46:22 +00:00
|
|
|
root->child[i]->fail = root;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!q.empty()) {
|
|
|
|
auto cur = q.front();
|
|
|
|
q.pop();
|
|
|
|
|
|
|
|
for (int i = 0; i < 26; i++) {
|
2022-12-31 09:15:29 +00:00
|
|
|
if (cur->child[i] != nullptr) {
|
|
|
|
cur->child[i]->fail = cur->fail->child[i] == nullptr ? root : cur->fail->child[i];
|
2022-09-29 12:46:22 +00:00
|
|
|
|
2022-12-31 09:15:29 +00:00
|
|
|
q.emplace(cur->child[i]);
|
|
|
|
} else {
|
|
|
|
cur->child[i] = cur->fail->child[i] == nullptr ? root : cur->fail->child[i];
|
2022-09-29 12:46:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int query(std::string t) {
|
|
|
|
int res = 0;
|
2022-12-31 09:15:29 +00:00
|
|
|
std::shared_ptr<node> cur = root;
|
2022-09-29 12:46:22 +00:00
|
|
|
|
|
|
|
for (char c : t) {
|
|
|
|
cur = cur->child[c - 'a'] == nullptr ? root : cur->child[c - 'a'];
|
|
|
|
|
2022-12-31 09:15:29 +00:00
|
|
|
for (std::shared_ptr<node> i = cur; i != nullptr && i->cnt != -1; i = i->fail) {
|
|
|
|
res += i->cnt;
|
|
|
|
i->cnt = -1;
|
2022-09-29 12:46:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int n;
|
|
|
|
std::string s, t;
|
|
|
|
AcAutomaton ac;
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
std::ios::sync_with_stdio(false);
|
|
|
|
cin.tie(nullptr);
|
|
|
|
|
|
|
|
cin >> n;
|
|
|
|
|
|
|
|
for (int i = 1; i <= n; i++) {
|
|
|
|
cin >> s;
|
|
|
|
|
|
|
|
ac.insert(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
ac.build();
|
|
|
|
|
|
|
|
cin >> t;
|
|
|
|
|
|
|
|
cout << ac.query(t) << endl;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|