这题是多例。。。
#include <cstdio>
#include <cstring>
#include <queue>
#include <cstdlib>
using namespace std;
const int maxn = 30000;
int Node[maxn][3], link[maxn], fail[maxn];
char elem[maxn];
int cnt[1010];
char pt[1010][60];
int top;
queue <int> q;
int newNode(char ch)
{
top ++;
link[top] = Node[top][0] = Node[top][1] = Node[top][2] = 0;
elem[top] = ch;
return top;
}
void Insert(char str[], int id)
{
int p = 1, idx = 0;
while(str[idx])
{
if(!Node[p][2]) Node[p][2] = newNode(str[idx]);
p = Node[p][2];
while(elem[p] != str[idx])
{
if(str[idx] < elem[p])
{
if(!Node[p][0]) Node[p][0] = newNode(str[idx]);
p = Node[p][0];
}
else
{
if(!Node[p][1]) Node[p][1] = newNode(str[idx]);
p = Node[p][1];
}
}
idx ++;
}
link[p] = id;
}
int findFail(int p, int fp)
{
while(elem[p] != elem[fp])
{
if(elem[p] < elem[fp])
if(!Node[fp][0]) return 0;
else fp = Node[fp][0];
else
if(!Node[fp][1]) return 0;
else fp = Node[fp][1];
}
fail[p] = fp;
return 1;
}
int que[27], tl, hd;
void travelPre(int p, int pre)
{
tl = hd = 0;
que[++hd] = p;
while(tl != hd)
{
p = que[++tl];
q.push(p);
int tmp = fail[pre];
while(tmp && !findFail(p, Node[tmp][2])) tmp = fail[tmp];
if(tmp == 0) fail[p] = 1;
if(Node[p][0]) que[++hd] = Node[p][0];
if(Node[p][1]) que[++hd] = Node[p][1];
}
}
void getFail()
{
while(!q.empty()) q.pop();
q.push(1);
int p;
fail[1] = 0;
while(!q.empty())
{
p = q.front();
q.pop();
if(Node[p][2])
travelPre(Node[p][2], p);
}
}
char ss[2000010];
int matchCH(int p, char ch, int &pre)
{
while(ch != elem[p])
{
if(ch < elem[p])
if(!Node[p][0])return 0;
else p = Node[p][0];
else
if(!Node[p][1]) return 0;
else p = Node[p][1];
}
pre = p;
while(p)
{
if(link[p])
cnt[link[p]] ++;
p = fail[p];
}
return 1;
}
void match()
{
int idx = 0, p = 1;
while(ss[idx])
{
while(p && !matchCH(Node[p][2], ss[idx], p)) p = fail[p];
if(p == 0) p = 1;
idx ++;
}
}
int main()
{
int n, i;
while(~scanf("%d",&n))
{
top = 0;
newNode('\0');
for(i = 1; i <= n; i++)
{
scanf("%s", pt[i]);
Insert(pt[i], i);
cnt[i] = 0;
}
getFail();
scanf("%s", ss);
match();
for(i = 1; i <= n; i++)
if(cnt[i])
printf("%s: %d\n", pt[i], cnt[i]);
}
return 0;
}