hdu2846 Repository (字典树)

来源:互联网 发布:网络接入服务 编辑:程序博客网 时间:2024/05/01 15:52

Repository

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


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

这道题有点不一样,因为其中只要存在字串就行,而不是前缀,考虑到n比较小,而且每个输入也只有20个字符,我们考虑拆分,这样就是最大的2e5的数据,是可以跑的过去的,然后我们主要是处理一个问题,就是面对aaaaa这样的数据,如果查询的是a,那么就会找到5次,这明显就不对了,所以我们考虑。
我们使用一个标记,对于每个字串,我们做一个标记,只有当这个字串不为其它串的字串时,我们才++t->num这个标记。

这道题要用C++才能避免MLE,或者直接搞静态的。

/************************** *Create time: Sat Jul 30 14:33:58 2016 *Author: Mymilkbottles *File name: HDU2846**************************/#include<iostream>using namespace std;#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<stdlib.h>#include<vector>#include<queue>#include<deque>#include<map>#include<set>#include<time.h>#define pi(x,y) printf("%d%c",(x),(y));#define pin(x) printf("%d\n",(x));#define si(x) scanf("%d",&(x))#define sii(x,y) scanf("%d%d",&(x),&(y))#define s3(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))#define rep(x,y,z) for(int (x)=(y);(x)<(z);++(x))#define dep(x,y,z) for(int (x)=(y)-1;(x)>=(z);--(x))#define read int TcaseN;scanf("%d",&TcaseN);for(int Tcase=1;Tcase<=TcaseN;++Tcase)#define cls(x,y) memset((x),(y),sizeof((x)));#define pb(x) push_back(x)#define mp(x,y) make_pair((x),(y))#define max3(value_a,value_b,value_c) max(max(value_a,value_b),value_c)#define min3(value_a,value_b,value_c) min(min(value_a,value_b),value_c)#define GT(x) (x)=clock();///In This You Can Define Long Integer Type#define LONGTYPE long longtypedef LONGTYPE LL;typedef unsigned LONGTYPE ULL;const int maxint=((~((unsigned)(0)))>>1);const LL maxll=((~((unsigned LONGTYPE)(0)))>>1);const int inf=0x3f3f3f3f;const double PI=acos(-1.0);//const int maxn=1e3+5;struct note {    int num,id;    struct note*next[26];    note() {        rep(i,0,26)next[i]=NULL;        num=0;id=0;    }};void insert_Trie(note* t,char* in,int Q) {    int len=strlen(in);    note* p= t;    rep(j,0,len) {        t=p;        rep(i,j,len) {//            if(j>0&&in[j]==in[j-1])continue;            int pos=in[i]-'a';            if(t->next[pos]==NULL) {                note* temp=new note();                t->next[pos]=temp;            }            t=t->next[pos];            if(t->id^Q)++t->num,t->id=Q;        }    }}void query_Trie(note *t,char *in) {    int len=strlen(in);    rep(i,0,len) {        int pos=in[i]-'a';        if(t->next[pos]==NULL) {            puts("0");            return ;        }        t=t->next[pos];    }    printf("%d\n",t->num);}void delete_Trie(note *t) {    rep(i,0,26) {        if(t->next[i]) {            delete_Trie(t->next[i]);        }    }    delete t;}int main() {#ifdef tangge    clock_t tSTART,tEND,t3;    GT(tSTART);#endif // tangge    char in[22];    int n,Q;    si(n);    note *t=new note();    rep(i,0,n) {        scanf("%s",in);        insert_Trie(t,in,i+1);    }    si(Q);    while(Q--) {        scanf("%s",in);        query_Trie(t,in);    }    delete_Trie(t);#ifdef tangge    GT(tEND);    printf("%.8lf\n",(tEND-tSTART)/1000.0);#endif // tangge    return 0;}
0 0
原创粉丝点击