再敲2222~
纯数组版AC自动机
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<cstring>
#include<iomanip>
using namespace std;
#ifdef _WIN32
#define i64 __int64
#define out64 "%I64d\n"
#define in64 "%I64d"
#else
#define i64 long long
#define out64 "%lld\n"
#define in64 "%lld"
#endif
#define FOR(i,a,b) for( int i = (a) ; i <= (b) ; i ++)
#define FF(i,a) for( int i = 0 ; i < (a) ; i ++)
#define FFD(i,a) for( int i = (a)-1 ; i >= 0 ; i --)
#define S64(a) scanf(in64,&a)
#define SS(a) scanf("%d",&a)
#define LL(a) ((a)<<1)
#define RR(a) (((a)<<1)+1)
#define pb push_back
#define CL(Q) while(!Q.empty())Q.pop()
#define MM(name,what) memset(name,what,sizeof(name))
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
const int inf = 0x3f3f3f3f;
const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 1e-10;
const double pi = acos(-1.0);
const int maxc = 26;
const int maxn = 1000000;
const int head = 0;
string s,ring;
struct zz
{
int to[26];
int fail;
int you;
}zx[maxn];
bool vis[maxn];
queue<int>q;
int n,use;
int ans;
inline int get()
{
use++;
zx[use].fail = 0;
zx[use].you = 0;
MM(zx[use].to,-1);
return use;
}
void insert(string& p)
{
int now = head;
int c;
for(int i=0;i<p.length();i++)
{
c = p[i]-'a';
if(zx[now].to[c]!=-1)
{
now = zx[now].to[c];
}
else
{
zx[now].to[c] = get();
now = zx[now].to[c];
}
}
zx[now].you++;
return ;
}
void ac()
{
CL(q);
q.push(head);
int now,to,temp;
while(!q.empty())
{
now = q.front();
q.pop();
for(int i=0;i<maxc;i++)
{
if(zx[now].to[i] != -1)
{
to = zx[now].to[i];
q.push(to);
temp = now;
while(temp)
{
temp = zx[temp].fail;
if(zx[temp].to[i]!=-1)
{
zx[to].fail = zx[temp].to[i];
break;
}
}
}
}
}
return ;
}
void find(int now)
{
while(now && !vis[now])
{
vis[now] = true;
ans += zx[now].you;
now = zx[now].fail;
}
return ;
}
void ak()
{
int now = head;
int temp;
int c;
for(int i=0;i<ring.length();i++)
{
c = ring[i]-'a';
if(zx[now].to[c] != -1)
{
now = zx[now].to[c];
find(now);
}
else
{
temp = now;
while(now)
{
now = zx[now].fail;
if(zx[now].to[c]!=-1)
{
now = zx[now].to[c];
break;
}
}
zx[temp].to[c] = now;
find(now);
}
}
return ;
}
void start()
{
ac();
ak();
return ;
}
int main()
{
int T;
cin>>T;
while(T--)
{
cin>>n;
use=-1;
ans=0;
get();
for(int i=1;i<=n;i++)
{
cin>>s;
insert(s);
}
for(int i=0;i<=use;i++)
{
vis[i] = false;
}
cin>>ring;
start();
cout<<ans<<endl;
}
return 0;
}