#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;
const int maxn = 1e6 + 7;
int cnt = 0;
struct ACNode {
int fail, num;
int vis[26];
}ac[maxn];
void Insert(string &input) {
int cur = 0;
for (int i = 0; i < input.size(); i++) {
int index = input[i] - 'a';
if (ac[cur].vis[index] == 0)ac[cur].vis[index] = ++cnt;
cur = ac[cur].vis[index];
}
ac[cur].num++;
}
void GetFail() {
int cur = 0;
queue<int> q;
for (int i = 0; i < 26; i++) {
if (ac[cur].vis[i]) {
int index = ac[cur].vis[i];
ac[index].fail = 0;
q.push(index);
}
}
while (!q.empty()) {
cur = q.front();
q.pop();
for (int i = 0; i < 26; i++) {
if (ac[cur].vis[i]) {
int son = ac[cur].vis[i];
ac[son].fail = ac[ac[cur].fail].vis[i];
q.push(son);
}
else {
ac[cur].vis[i] = ac[ac[cur].fail].vis[i];
}
}
}
}
int query(string &s)
{
int len = s.length();
int cur = 0, ans = 0;
for (int i = 0; i < len; ++i)
{
cur = ac[cur].vis[s[i] - 'a'];
ans += ac[cur].num;
ac[cur].num = 0;
}
return ans;
}
int main()
{
int n;
string s;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s;
Insert(s);
}
ac[0].fail = 0;
GetFail();
cin >> s;
cout << query(s) << endl;
return 0;
}
|