HDU 2864 Repository 字典树

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
2009 Multi-University Training Contest 4 - Host by HDU
 

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
 */


你可能感兴趣的:(C++,HDU,字典树)