2014
02-17

# Repository

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.

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.

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.

20
ae
af
ag
ah
ai
aj
ak
al
aes
5
b
a
d
s

0
20
11
11
2

#include <list>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <deque>
#include <stack>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <limits.h>
#include <time.h>
#include <string.h>
using namespace std;

#define LL long long
#define PI acos(-1.0)
#define Max INT_MAX
#define Min INT_MIN
#define eps 1e-8
#define FRE freopen("a.txt","r",stdin)

struct Node{
Node *next[26];
int inid;
int pass;
Node(){
pass=0;
inid=0;
for(int i=0;i<26;i++)
next[i]=NULL;
}
};
void add(char *str,int beg,int si,Node *root){
int i,j;
Node *pos=root;
for(i=beg;str[i];i++){
int id=str[i]-'a';
if(pos->next[id]==NULL)
pos->next[id]=new Node();

pos=pos->next[id];
if(pos->inid==si)continue;
pos->inid=si;
pos->pass++;
}
}
void substr(char *str,int si,Node *&root){
int i,j;
for(i=0;str[i];i++){
}
}
void find(Node *root,char *str){
int i,j;
Node *pos=root;
for(i=0;str[i];i++){
int id=str[i]-'a';
if(pos->next[id]==NULL){
printf("0\n");
return ;
}
pos=pos->next[id];
}
printf("%d\n",pos->pass);
}
void release(Node *root)
{
int i;
for (i = 0; i < 26; ++i)
{
if (root->next[i] != NULL)
{
release(root->next[i]);
}
}
delete (root);
}

int main(){
int n;
char str[22];
while(scanf("%d",&n)!=EOF){
int i,j;
Node *root = new Node();
for(i=1;i<=n;i++){
scanf("%s",str);
substr(str,i,root);
}
int m;
scanf("%d",&m);
while(m-- && scanf("%s",str)){
find(root,str);
}
release(root);
}
return 0;
}