hdu1251统计难题

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 using namespace std;
 5 struct node{
 6     int count;
 7     node *next[26]; 
 8     node(){  //初始化数据 
 9         memset(next,NULL,sizeof(next));
10         count=0;
11     }
12 };
13 node *p,*root=new node();
14 void insert(char *s)//插入新单词 
15 {
16     int i,k;
17     for(p=root,i=0;s[i];++i)
18     {
19         k=s[i]-'a';
20         if(p->next[k]==NULL) p->next[k]=new node();
21         p=p->next[k];
22         p->count++;  //记录此字母出现的次数 
23     }
24 }
25 int search(char *s)
26 {
27     int i,k;
28     for(p=root,i=0;s[i];++i)
29     {
30         k=s[i]-'a';
31         if(p->next[k]==NULL) break; //一旦查找不到,立即跳出 
32         p=p->next[k];
33     }
34     if(s[i]) return 0;//s[i]!=0表示中间 
35     return p->count;
36 }
37 int main()
38 {
39     char s[11];
40     while(gets(s),*s) insert(s); //一直读入数据,直到遇到空字符串 
41     while(gets(s))
42         printf("%d\n",search(s));
43     return 0;
44 } 

 

posted on 2012-08-09 17:27  小花熊  阅读(266)  评论(0编辑  收藏  举报

导航