首页 > 其他 > 详细

HDU 2846 Repository(字典树变形)

时间:2014-09-03 22:44:17      阅读:414      评论:0      收藏:0      [点我收藏+]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2846

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


代码如下:

#include <cstdio>
#include <cstring>
#include <malloc.h>
#include <iostream>
using namespace std;
#define MAXN 26
typedef struct Trie
{
    int IDD;//最后一次经过此结点的商品ID
    int v;//根据需要变化
    Trie *next[MAXN];
    //next是表示每层有多少种类的数,如果只是小写字母,则26即可,
    //若改为大小写字母,则是52,若再加上数字,则是62了
} Trie;
Trie *root;

void createTrie(char *str, int IDD)
{
    int len = strlen(str);
    Trie *p = root, *q;
    for(int i = 0; i < len; i++)
    {
        int id = str[i]-'a';
        if(p->next[id] == NULL)
        {
            q = (Trie *)malloc(sizeof(Trie));
            //   q->v = 1;//初始v==1
            q->v = 0;
            for(int j = 0; j < MAXN; j++)
                q->next[j] = NULL;
            q->IDD = -1;
            p->next[id] = q;
            //   p = p->next[id];
        }
        /* else
         {
             p->next[id]->v++;
             p = p->next[id];
         }*/
        p = p->next[id];
        if(p->IDD != IDD)
        {
            p->IDD = IDD;
            p->v++;
        }
    }
}

int findTrie(char *str)
{
    int len = strlen(str);
    Trie *p = root;
    for(int i = 0; i < len; i++)
    {
        int id = str[i]-'a';
        p = p->next[id];
        if(p == NULL) //若为空集,表示不存以此为前缀的串
            return 0;

    }
    return p->v;
}
int main()
{
    int n, m;
    char str[MAXN];
    root = (Trie *)malloc(sizeof(Trie));
    for(int i = 0; i < MAXN; i++)
        root->next[i] = NULL;
    root->v=0;
    root->IDD=-1;
    scanf("%d",&n);
    for(int i = 0; i < n; i++)
    {
        scanf("%s",str);
        for(int j = 0; j < strlen(str); j++)       //将字符串X=X1X2...Xn的分别以X1,X2...Xn开头的后缀字符串插入到Trie树中
        {
            createTrie(str+j,i);
        }
    }
    scanf("%d",&m);
    for(int i = 0; i < m; i++)
    {
        scanf("%s",str);
        printf("%d\n",findTrie(str));
    }
    return 0;
}


HDU 2846 Repository(字典树变形)

原文:http://blog.csdn.net/u012860063/article/details/39034553

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!