• hdu2846


    Repository

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


    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
     
    思路:把每个单词X1X2X3……分别以X1,X2,X3为开头存入字典树中,然后要进行标记,同一个单词不能多次计数
    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<malloc.h>
    #define MAX 26
    using namespace std;
    struct tree
    {
        struct tree *child[26];
        int v;//记录包含该结点的单词个数
        int flag; //最后一次经过此结点的商品ID
    }*root;
    void init()
    {
        int i;
        root=(struct tree*)malloc(sizeof(struct tree));
        for(i=0; i<26; i++)
        {
            root->child[i]=0;
            root->v=0;
            root->flag=-1;
        }
        return ;
    }
    void build(char *str,int d)
    {
        int id,i,j,len;
        len=strlen(str);
        struct tree *p=root,*q;
        for(i=0; i<len; i++)
        {
            id=str[i]-'a';
            if(p->child[id]==0)
            {
                q=(struct tree*)malloc(sizeof(struct tree));
                for(j=0; j<26; j++)
                    q->child[j]=0;
                p->child[id]=q;
                p=p->child[id];
                p->v=0;
                p->flag=-1;
            }
            else
                p=p->child[id];
            if(p->flag!=d)//如果当前结点的商品编号不等于要插入商品的编号,则计数器v++,并且重新置编号的值
            {
                p->flag=d;
                p->v++;
            }
        }
    }
    int find(char *str)
    {
        int len=strlen(str);
        int i,j,id;
        struct tree *p=root,*q;
        for(i=0; i<len; i++)
        {
            id=str[i]-'a';
            if(p->child[id]==0)
                return 0;
            p=p->child[id];
        }
        return p->v;
    }
    int main()
    {
        int i,j,len,tt,t;
        char a[105],b[105];
        scanf("%d",&tt);
        init();
        for(i=0;i<tt;i++)
        {
            scanf("%s",b);
            len=strlen(b);
            for(j=0; j<len; j++)
                build(b+j,i);//将字符串X=X1X2...Xn的分别以X1,X2...Xn开头的后缀字符串插入到tree树中
        }
        scanf("%d",&t);
        for(i=0; i<t; i++)
        {
            scanf("%s",a);
            printf("%d ",find(a));
        }
        return 0;
    }
  • 相关阅读:
    排序
    FileOutputStream文件写入 —— 覆盖、追加
    判断一个地图坐标是否在中国镜内
    地图坐标转换
    全文检索 使用最新lucene3.0.3+最新盘古分词 pangu2.4 .net 实例
    blob转base64位 base64位转blob
    div中粘贴图片并上传服务器 div中拖拽图片文件并上传服务器
    DIV 粘贴插入文本或者其他元素后,移动光标到最新处
    project 计划添加编号或 任务分解时为任务添加编号
    修改project任务的默认开始时间
  • 原文地址:https://www.cnblogs.com/lxm940130740/p/3569576.html
Copyright © 2020-2023  润新知