HDU 1251

用字典树统计个数。

一定要交C++。G++MLE。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <string>
 5 #include <cstdlib>
 6 using namespace std;
 7 struct Trie
 8 {
 9     Trie *next[26];
10     int count;
11     Trie()
12     {
13         count=0;
14         for(int i=0;i<26;i++) next[i]=NULL;
15     }
16 };
17 void insert(Trie *s,char x[])
18 {
19     int k;
20     for(int i=0;x[i];i++)
21     {
22         k=x[i]-'a';
23         if(s->next[k]==NULL)
24             s->next[k]=new Trie;
25         s=s->next[k];
26         ++s->count;
27     }
28 }
29 int find(Trie *s,char x[])
30 {
31     int k;
32     for(int i=0;x[i];i++)
33     {
34         k=x[i]-'a';
35         if(s->next[k]) s=s->next[k];
36         else return 0;
37     }
38     return s->count;
39 }
40 int main()
41 {
42     int num=0;
43     Trie* root=new Trie;
44     char s[20];
45     while(gets(s),strcmp(s,""))
46     {
47         insert(root,s);
48     }
49     while(gets(s)!=NULL)
50     {
51         printf("%d
",find(root,s)) ;
52     }
53 }
我自倾杯,君且随意
原文地址:https://www.cnblogs.com/nicetomeetu/p/5682763.html