题意:给出一些短文,然后一些个搜索串,问有多少个文章包含这个搜索串
解法: 字典树,对于长度为n的短文,要向字典树插入n次,第一次为[1,n],第二次[2,n],...第n次[n,n]。
每个结点要标记有多少短文经过此结点(用一个标记记录上一次的短文)。
Repository
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 5419 Accepted Submission(s): 1819
Problem Description
When you go shopping, you can search in repository for avalible merchandises by the computers and internet. First you give the search system a name about something, then the system responds with the results. Now you are given a lot merchandise names in repository and some queries, and required to simulate the process.
Input
There is only one case. First there is an integer P (1<=P<=10000)representing the number of the merchanidse names in the repository. The next P lines each contain a string (it's length isn't beyond 20,and all the letters are lowercase).Then there is an integer Q(1<=Q<=100000) representing the number of the queries. The next Q lines each contains a string(the same limitation as foregoing descriptions) as the searching condition.
Output
For each query, you just output the number of the merchandises, whose names contain the search string as their substrings.
Sample Input
20 ad ae af ag ah ai aj ak al ads add ade adf adg adh adi adj adk adl aes 5 b a d ad s
Sample Output
0 20 11 11 2
Source
Recommend
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define mes(a,x,s) memset(a,x,(s)*sizeof a[0])
#define mem(a,x) memset(a,x,sizeof a)
#define ysk(x) (1<<(x))
typedef long long ll;
//const int INF = 0x3f3f3f3f ;
const int maxn= 10000;
const int maxm= 100000;
int n,m;
char word[25];
struct Node
{
int nex[26],cnt,last;
void clear()
{
mes(nex,-1,26);
cnt=0;
last=-1;
}
}nodes[3000000];
struct Tree
{
int root,nnode;
int newNode()
{
nodes[nnode++].clear();
return nnode-1;
}
void init()
{
nnode=0;
root=newNode();
}
void insert(char *s,int ind)
{
int now=root;
for(int i=0;s[i];i++)
{
int y=s[i]-'a';
if(nodes[now].nex[y]==-1) nodes[now].nex[y]=newNode();
now=nodes[now].nex[y];
if(nodes[now].last!=ind ) nodes[now].cnt++;
nodes[now].last=ind;
}
}
int find(char *s)
{
// printf("find %s\n",s);
int now=root;
for(int i=0;s[i];i++)
{
int y=s[i]-'a';
// printf("->%c\n",s[i]);
if(nodes[now].nex[y]==-1) return 0;
now=nodes[now].nex[y];
}
return nodes[now].cnt;
}
}dict;
int main()
{
dict.init();
scanf("%d",&n);
for1(i,n)
{
scanf("%s",word);
int len=strlen(word);
for(int j=0;j<len;j++)
{
dict.insert(word+j,i);
}
}
scanf("%d",&m);
for1(i,m)
{
scanf("%s",word);
printf("%d\n",dict.find(word));
}
return 0;
}