UVa 11488

找 前缀长度*符合该前缀的字符串数 的最大值

顺便练了一下字典树的模板

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib> 
 5 using namespace std;
 6 struct trie{
 7     trie *next[2];
 8     int index;//数量 
 9 };
10 inline trie* newnode()
11 {
12     trie *t;
13     t=(trie*)malloc(sizeof(trie));
14     memset(t,0,sizeof(trie));//    !!!!!
15     return t;
16 }
17 int t,n,ans;
18 char s[205];
19 void insert(trie *s,char x[])
20 {
21     int i,k; 
22     trie *t;
23     for(i=0;x[i];i++)
24     {
25         k=x[i]-'0';
26         if(s->next[k])s=s->next[k];
27         else{
28             t=newnode();
29             s->next[k]=t;
30             s=t;
31         }
32         s->index++;
33     }
34 }
35 void find(trie *s,int x)
36 {
37     int i,k;
38     for(i=0;i<2;i++)
39     {
40         if(s->next[i]){
41             ans=max(ans,s->next[i]->index*x); 
42             find(s->next[i],x+1);//向下找 
43         }
44     }
45 }
46 int main()
47 {
48     scanf("%d",&t);
49     while(t--)
50     {
51         scanf("%d",&n);
52         trie* root=newnode();
53         for(int i=1;i<=n;++i)
54         {
55             scanf("%s",s);
56             insert(root,s);
57         }
58         ans=0;
59         find(root,1);
60         printf("%d
",ans);
61     }
62 }
我自倾杯,君且随意
原文地址:https://www.cnblogs.com/nicetomeetu/p/5494232.html