HDU 2864 Repository 字典树

本文介绍了一种使用前缀树(Trie树)进行字符串匹配的方法,通过构建商品名称的前缀树并存储其子串,实现高效搜索含有特定子串的商品。文章详细解释了Trie树的创建和查询过程,并提供了完整的C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Repository

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


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
 

Recommend
gaojie   |   We have carefully selected several similar problems for you:   2852  2847  2845  2850  2851 
 

给出n个串然后再询问q个串在前n个串出现的次数

因为只要出现就算那么建树的时候把串的子串也存入

因为会出现一个串中有重复出现查询串的情况所以建树的时候要标记一下

G++会超内存c++不会但是会稍微慢一点

ACcode:

#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define maxn 100005
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI  acos(-1.0)
#define E  exp(1)
#define MAX 26
using namespace std;
struct Trie{
    Trie *next[MAX];
    int cnt;
    int flag;
};
Trie *root;
int cnt,n,q;
void creatTrie(char *str,int st,int len,int loop){
    Trie *p=root,*q;
    FOR(i,st,len-1){
        int id=str[i]-'a';
        if(p->next[id]==NULL){
            q=(Trie*)malloc(sizeof(Trie));
            q->cnt=1;
            q->flag=loop;
            FOR(j,0,MAX-1)q->next[j]=NULL;
            p->next[id]=q;
            p=p->next[id];
        }
        else {
                p=p->next[id];
                if(p->flag!=loop){
                        p->flag=loop;
                p->cnt++;
                }
        }
    }
}
int  find_Trie(char *str){
    int len=strlen(str);
    Trie *p=root;
    FOR(i,0,len-1){
        int id=str[i]-'a';
        p=p->next[id];
        if(p==NULL)return 0;
    }
    return p->cnt;
}
void deal(Trie *t){
    if(t==NULL)return ;
    FOR(i,0,MAX-1)
        if(t->next[i])
            deal(t->next[i]);
    free(t);
}
char str[50];
int main(){
    while(rd(n)!=EOF){
        root=(Trie *)malloc(sizeof(Trie));
        FOR(i,0,MAX-1)root->next[i]=NULL;
        FOR(i,0,n-1){
            rds(str);
            int len=strlen(str);
            for(int j=0;j<len;++j)
            creatTrie(str,j,len,i);
            }
        rd(q);FOR(i,1,q){rds(str);cout<<find_Trie(str)<<'\12';}
        deal(root);
    }
    return 0;
}
/*
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
 */


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值