第一次写AC自动机。
#ifdef _DEBUG
#pragma warning(disable : 4996)
#endif
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <sstream>
#include <utility>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cctype>
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define CLOSE() ios::sync_with_stdio(false)
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define PF(a) printf("%d\n", a)
#define SF(a) scanf("%d", &a)
#define SFF(a, b) scanf("%d%d", &a, &b)
#define SFFF(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define LL long long
#define maxn 100005
#define maxm 10005
#define MOD 10007
#define INF 10007
using namespace std;
//-------------------------CHC------------------------------//
struct Node {
int end = 0;
Node *son[26], *fail;
Node() {
fail = NULL;
FOR(i, 0, 26) son[i] = NULL;
}
};
void add(char *s, Node *root) {
Node *cur = root;
while (*s) {
int id = *s - 'a';
if (!cur->son[id]) cur->son[id] = new Node;
cur = cur->son[id];
++s;
}
cur->end++;
}
void GetFail(Node *root) {
queue<Node *> q;
FOR(i, 0, 26) if (root->son[i])
root->son[i]->fail = root, q.push(root->son[i]);
while (q.size()) {
Node *parent = q.front();
q.pop();
FOR(i, 0, 26) {
if (parent->son[i]) {
Node *son = parent->son[i];
q.push(son);
Node *t = parent->fail;
while (t) {
if (t->son[i]) {
son->fail = t->son[i];
break;
}
t = t->fail;
}
if (!t) son->fail = root;
}
}
}
}
int solve(char *s, Node *root) {
int ret = 0;
Node *pre = root, *cur;
while (*s) {
int id = *s - 'a';
if (pre->son[id]) {
cur = pre->son[id];
while (cur != root) {
if (cur->end >= 0) ret += cur->end, cur->end = -1;
else break;
cur = cur->fail;
}
pre = pre->son[id];
++s;
}
else {
if (pre == root) ++s;
else pre = pre->fail;
}
}
return ret;
}
void _free(Node *root) {
if (root) {
FOR(i, 0, 26) _free(root->son[i]);
delete root;
}
}
char s[1000005], t[55];
int main() {
int T;
SF(T);
while (T--) {
Node *root = new Node;
int n;
SF(n);
FOR(i, 0, n) {
scanf("%s", t);
add(t, root);
}
scanf("%s", s);
GetFail(root);
PF(solve(s, root));
_free(root);
}
return 0;
}