Problem Description


In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
Wiskey also wants to bring this feature to his image retrieval system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.


Input


First line will contain one integer means how many cases will follow by.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.


Output


Print how many keywords are contained in the description.


Sample Input


1 5 she he say shr her yasherhs


Sample Output


3


ac自动机练手题。

#include<stdio.h>
#include<string.h>
#include<queue>
#include<malloc.h>
using namespace std;
const int maxn = 1000005;
int n, T;

class tire
{
public:
tire *down[26], *next;
int end;
tire(){ next = NULL; end = 0; memset(down, 0, sizeof(down)); }
void insert(int &x, tire &y){ down[x] = &y; }
};

class ac_automaton
{
private:
tire *root;
char S[maxn];
public:
ac_automaton(){ root = new tire; }
void insert()
{
scanf("%s", S);
tire* j = root;
for (int i = 0, k; S[i]; i++)
{
k = S[i] - 'a';
if (!j->down[k]) j->down[k] = new tire;
j = j->down[k];
}
j->end++;
}

void getnext()
{
tire *q, *j, *k;
queue<tire*> p;
p.push(root);
while (!p.empty())
{
q = p.front(); p.pop();

for (int i = 0; i < 26; i++)
if (q->down[i])
{
j = q->down[i];
k = q->next;
while (k && !k->down[i]) k = k->next;
if (k) j->next = k->down[i]; else j->next = root;
p.push(j);
}
}
}

int work_out()
{
getnext();
int ans = 0;
scanf("%s", S);
tire*j = root, *u;
for (int i = 0, k; S[i]; i++)
{
k = S[i] - 'a';
while (j && !j->down[k]) j = j->next;

if (j) j = j->down[k]; else j = root;

for (u = j; u != root && u->end >= 0; u = u->next)
{
ans += u->end; u->end = -1;
}
}
return ans;
}
};


int main()
{
scanf("%d", &T);
while (T--)
{
ac_automaton f;
scanf("%d", &n);
while (n--) f.insert();
printf("%d\n",f.work_out());
}
return 0;
}


#include<stdio.h>
#include<string.h>
#include<queue>
#include<malloc.h>
using namespace std;
const int maxn = 1000005;

class tire
{
public:
tire *down[26], *next;
int end;
tire(){ next = NULL; end = 0; memset(down, 0, sizeof(down)); }
void assign(){ next = NULL; end = 0; memset(down, 0, sizeof(down)); }
};

class ac_automaton
{
private:
tire *root;
char s[maxn];
char S[maxn];
public:
ac_automaton(){ root = new tire; }
void clear(){ root = new tire; }
void insert()
{
scanf("%s", s);
tire* j = root;
for (int i = 0, k; s[i]; i++)
{
k = s[i] - 'a';
if (!j->down[k]) j->down[k] = new tire;
j = j->down[k];
}
j->end++;
}

void getnext()
{
queue<tire*> p;
tire *q, *k, *j;

root->next = root;
for (int i = 0; i < 26; i++)
if (root->down[i])
{
q = root->down[i];
q->next = root;
p.push(q);
}
else root->down[i] = root;

while (!p.empty())
{
q = p.front(); p.pop();
k = q->next;

for (int i = 0; i < 26; i++)
if (q->down[i])
{
j = q->down[i];
j->next = k->down[i];
p.push(q->down[i]);
}
else q->down[i] = k->down[i];
}
}

void work_out()
{
int ans = 0;
getnext();
scanf("%s", S);
tire *j = root, *u;
for (int i = 0, k; S[i]; i++)
{
k = S[i] - 'a';
j = j->down[k];
for (u = j; u != root&&u->end >= 0; u = u->next)
{
ans += u->end; u->end = -1;
}
}
printf("%d\n", ans);
}
};

ac_automaton f;
int T, n;

int main()
{
scanf("%d", &T);
while (T--)
{
f.clear();
scanf("%d", &n);
while (n--) f.insert();
f.work_out();
}
return 0;
}