Qiuqiqiu  
不管道路多么崎岖坎坷,我永远不停下追逐梦想的脚步!

http://acm.hdu.edu.cn/showproblem.php?pid=1251

字典树

View Code
 1 #include <stdio.h>
2 #include <string.h>
3
4 const int N=500010;
5 struct node
6 {
7 int nex[26];
8 int cnt;
9 }tr[N];
10 int sz;
11 void insert(char *s)
12 {
13 int k=0; tr[k].cnt++;
14 int i;
15 for (i=0;s[i];i++)
16 {
17 int c=s[i]-'a';
18 int &p=tr[k].nex[c];
19 if (!p) p=++sz;
20 k=p;
21 tr[k].cnt++;
22 }
23 }
24 int find(char *s)
25 {
26 int p=0,i;
27 for (i=0;s[i];i++)
28 if (!(p=tr[p].nex[s[i]-'a'])) return 0;
29 return tr[p].cnt;
30 }
31 int main()
32 {
33 char w[15];
34 memset(tr,0,sizeof(tr)); sz=0;
35 while (gets(w),w[0]) insert(w);
36 while (gets(w)) printf("%d\n",find(w));
37 return 0;
38 }

 

posted on 2012-03-12 21:44  Qiuqiqiu  阅读(250)  评论(0编辑  收藏  举报