杭电2846

Repository

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2109    Accepted Submission(s): 779


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
 
这个题目我开始想法就是先把树建起来,然后再在树中查找,但发现查找的那个函数会很复杂。。
借鉴其他人的方法是:把每个字符串的每一个子串在树中建立起来,那么查找的时候就会相对方便一些

#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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章