HDU2846 Repository 字典树

10 篇文章 0 订阅

题目链接:HDU2846

Repository

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


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


题意:给一组字符串和一组模式串,问模式串在前面字符串中共出现多少次。

题目分析:字典树就可以了,不同的是对于同一个字符串进行插入操作时每次都向前进一位到末尾组成新串再进行插入。注意注意如aaa这种字符串在插入计数的时候可能会算重复,所以对每一个插入的字符串需要标记一下。


还有就是提交代码最好要C++,我之前提交好几次G++都MLE了,换成C++立刻AC

//
//  main.cpp
//  HDU2846
//
//  Created by teddywang on 16/3/29.
//  Copyright © 2016年 teddywang. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef struct node{
    int flag;
    int num;
    node *next[26];
}trienode;
trienode *root;
char s[26];
int num,num2;

void insert_node(char *s,int num)
{
    trienode *r,*t=root;
    int len=strlen(s);
    for(int i=0;i<len;i++)
    {
        int buf=s[i]-'a';
        if(t->next[buf]==NULL)
        {
            r=new node;
            for(int j=0;j<26;j++)
            {
                r->next[j]=NULL;
            }
            r->flag=num;
            r->num=1;
            t->next[buf]=r;
            t=t->next[buf];
        }
        else
        {
            t=t->next[buf];
            if(t->flag!=num)
            {
                t->num=t->num+1;
                t->flag=num;
            }
        }
    }
}

int find(char *s)
{
    trienode *t=root;
    int len=strlen(s);
    for(int i=0;i<len;i++)
    {
        int buf=s[i]-'a';
        if(t->next[buf]==NULL)
            return 0;
        else t=t->next[buf];
    }
    return t->num;
}

void del(trienode *root)
{
    for(int i=0;i<26;i++)
    {
        if(root->next[i]!=NULL)
            del(root->next[i]);
    }
    free(root);
}

int main()
{
    root=new node;
    root->flag=root->num=0;
    for(int i=0;i<26;i++)
    {
        root->next[i]=NULL;
    }
    cin>>num;
    for(int i=0;i<num;i++)
    {
        memset(s,0,sizeof(s));
        scanf("%s",s);
        int len=strlen(s);
        char t[26];
        strcpy(t,s);
        for(int j=0;j<len;j++)
        {
            insert_node(t+j,i);
        }
    }
    cin>>num2;
    for(int i=0;i<num2;i++)
    {
        char s2[26];
        scanf("%s",s2);
        cout<<find(s2)<<endl;
    }
    del(root);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值