Repository
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2109 Accepted Submission(s): 779
#include <cstdio>
#include <cstring>
#include <cstdlib>
struct node
{
node *x[26];
int count;
int num;
};
node *tree;
char stu[30];
int p,q;
int flag;
void create(node *&t)
{
int i;
t=(node *)malloc(sizeof(node));
t->count=0;
t->num=-1;
for(i=0;i<26;++i)
t->x[i]=NULL;
}
void insertTree(char *ch,int n)
{
// printf("ch %s n %d\n",ch,n);
node *f=tree;
int i,j;
int len=strlen(ch);
for(i=0;i<len;++i)
{
int p=ch[i]-'a';
if(f->x[p]==NULL)
{
f->x[p]=(node *)malloc(sizeof(node));
f=f->x[p];
f->count=0;
f->num=-1;
for(j=0;j<26;++j)
f->x[j]=NULL;
if(i==len-1)
{
f->count=1;
f->num=n;
}
}
else
{
f=f->x[p];
if((i==len-1) && (f->num!=n))
{
f->num=n;
f->count++;
}
}
}
}
int chaxun()
{
node *f=tree;
int i,len=strlen(stu);
// printf("stu %s len %d\n",stu,len);
for(i=0;i<len;++i)
{
int p=stu[i]-'a';
if(f->x[p]==NULL)
return 0;
f=f->x[p];
if(i==len-1)
return f->count;
}
}
void deleteTree(node *&tree)
{
node *f=tree;
int i;
for(i=0;i<26;++i)
{
if(f->x[i]!=NULL)
{
deleteTree(f->x[i]);
}
}
free(f);
}
int main()
{
int i,j,k,len;
char ch[30];
create(tree);
scanf("%d",&p);getchar();
// printf("tree %d\np %d\n",tree,p);
for(i=1;i<=p;++i)
{
scanf("%s",stu);getchar();
len=strlen(stu);
int u;
for(j=0;j<len;++j)
{
u=-1;
for(k=j;k<len;++k)
{
++u;
ch[u]=stu[k];
ch[u+1]='\0';
insertTree(ch,i);
}
}
}
scanf("%d",&q);getchar();
// printf("q %d\n",q);
for(i=1;i<=q;++i)
{
scanf("%s",stu);getchar();
flag=chaxun();
printf("%d\n",flag);
}
deleteTree(tree);
return 0;
}