思路:道题做从学AC自动机,到做了这个题,用了几天时间用map 映射 每个字符串的标号在字典数的每个单词结尾处,标记上该单词的标号并把相同的单词放到一个队列里</p剩下的就是AC 自动机了
#include<map>
#include<queue>
#include<stdint.h>
#include<stdio.h>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
#include<iostream>
#pragma comment(linker,"/STACK:1024000000,1024000000")
const int maxn = 1e6+10;
map<string ,int> m;
char str[maxn];
char a[200][200];
struct node
{
int next[maxn][26];
int end[maxn];
int fail[maxn];
int ans [1000];
int root;
int L ;
void init()
{
L = 0 ;
for(int i = 1; i<10000; i++)
{
ans[i]= 0;
}
root = newnode();
}
int newnode()
{
for(int i = 0; i<26; i++)
{
next[L][i] = -1;
}
end[L++]=0 ;
return L -1;
}
void insert()
{
int now = root;
int len = strlen(str);
for(int i = 0; i<len; i++)
{
if(next[now][str[i]-'a']==-1)
{
next[now][str[i]-'a'] = newnode();
}
now = next[now][str[i]-'a'];
}
end[now] = m[str];
}
void getfali()
{
queue<int>q;
fail[root] = root;
for(int i = 0 ; i<26; i++)
{
if(next[root][i]==-1)
{
next[root][i] =root;
}
else
{
fail[next[root][i]] = root ;
q.push(next[root][i]);
}
}
while(!q.empty())
{
int now = q.front();
q.pop();
for(int i = 0 ; i<26; i++)
{
if( next[now][i]==-1)
{
next[now][i] =next[fail[now]][i];
}
else
{
fail[next[now][i]]= next[fail[now]][i];
q.push(next[now][i]);
}
}
}
}
void search()
{
int len = strlen(str);
int now = root ;
for(int i = 0; i<len; i++)
{
now = next[now][str[i]-'a'];
int temp = now ;
while(temp!=root)
{
ans[end[temp]]++;
temp = fail[temp];
}
}
}
};
node ac;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int ggg[1000];
memset(ggg,0,sizeof(ggg));
ac.init();
int n;
scanf("%d",&n);
int cot = 1 ;
queue<int>q[200];
for(int i = 1; i<= n; i++)
{
scanf("%s",str);
strcpy(a[i],str);
if(!m[str])
{
m[str] = cot++;
q[m[str]].push(i);
ac.insert();
}
else
{
q[m[str]].push(i);
}
}
ac.getfali();
scanf("%s",str);
ac.search();
int maxv= -1;
for(int i = 1; i<cot; i++)
{
if(ac.ans[i]>maxv)
{
maxv = ac.ans[i];
}
}
printf("%d\n",maxv);
int flag[1000];
memset(flag,0,sizeof(flag));
int gg= 0 ;
for(int i = 1 ; i<cot; i++)
{
if(ac.ans[i]==maxv)
{
flag[gg++] = i;
}
}
int ax[1000];
memset(ax,0,sizeof(ax));
int wocao = 0;
for(int i = 0 ;i<gg;i++)
{
while(!q[flag[i]].empty())
{
ax[wocao++] = q[flag[i]].front();
q[flag[i]].pop();
}
}
sort(ax+1,ax+wocao);
for(int i = 0 ;i<wocao;i++)
{
puts(a[ax[i]]);
}
m.clear();
}
return 0 ;
}