题意:
中文题
思路:
不同的子串打上各自的id,跑匹配的时候统计一下就行。注意一个子串可能会多次出现在母串中
代码:
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <queue>
using namespace std;
struct Trie
{
int next[150000][128],fail[150000],end[150000];
int root,L;
int newnode()
{
for(int i = 0; i < 128; i++)
next[L][i] = -1;
end[L++] = -1;
return L-1;
}
void init()
{
L = 0;
root = newnode();
}
void insert(char buf[],int id)
{
int len = strlen(buf);
int now = root;
for(int i = 0; i < len; i++)
{
if(next[now][buf[i]] == -1)
next[now][buf[i]] = newnode();
now = next[now][buf[i]];
}
end[now] = id;
}
void build()
{
queue<int>Q;
fail[root] = root;
for(int i = 0; i < 128; 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 < 128; 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]);
}
}
}
int query(char buf[],int id)
{
int res[4];
bool mark[510] = {0};
int len = strlen(buf);
int now = root;
memset(res,0,sizeof(res));
for(int i = 0; i < len; i++)
{
now = next[now][buf[i]];
int temp = now;
while( temp != root )
{
if(end[temp] != -1){
if(!mark[end[temp]]){
res[++res[0]] = end[temp];
mark[end[temp]] = 1;
}
}
temp = fail[temp];
}
}
if(res[0]){
printf("web %d: ",id);
sort(res+1,res+res[0]+1);
for(int i = 1;i <= res[0];i++){
if(i == res[0])
printf("%d\n",res[i]);
else
printf("%d ",res[i]);
}
return 1;
}
return 0;
}
};
char buf[20000];
Trie ac;
int main()
{
int n,m;
while(~scanf("%d",&n)){
ac.init();
for(int i = 0; i < n; i++)
{
scanf("%s",buf);
ac.insert(buf,i+1);
}
ac.build();
scanf("%d",&m);
int tot = 0;
for(int i = 0;i < m;i++){
//test = ac;
scanf("%s",buf);
tot+=ac.query(buf,i+1);
}
printf("total: %d\n",tot);
}
return 0;
}