#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct Trie
{
int cur;
int deepth;
Trie *next[2];
Trie()
{
cur=0;
deepth=0;
}
}Trie;
Trie *root;
int ans=0;
int maxn(int a,int b)
{
return a>=b?a:b;
}
void buildroot()
{
root=(Trie *)malloc(sizeof(Trie));
root->next[0]=NULL;
root->next[1]=NULL;
root->cur=0;
root->deepth=0;
}
void buildTrie(char *str)
{
int len= strlen(str),i;
Trie *p= root, *q;
for(i=0; i<len; i++)
{
int num=str[i]-'0';
if(p->next[num]==NULL)
{
q=(Trie *)malloc(sizeof(Trie));
q->next[0]=NULL ;
q->next[1]=NULL;
q->cur=q->deepth=0;
q->cur++;
q->deepth=i+1;
p->next[num]=q;
p= p->next[num];
}
else
{
p->next[num]->cur++;
p->next[num]->deepth=i+1;
p = p->next[num];
}
}
}
void searchTrie(Trie *r)
{ ans=maxn(ans,r->cur*r->deepth);
if(r->next[0]==NULL&&r->next[1]==NULL)
return ;
if(r->next[0]!=NULL)
{
searchTrie(r->next[0]);
}
if(r->next[1]!=NULL)
{
searchTrie(r->next[1]);
}
}
int main()
{
int N,i;
char ss[205];
buildroot();
scanf("%d",&N);
for(i=1;i<=N;i++)
{
scanf("%s",ss);
buildTrie(ss);
}
searchTrie(root);
printf("%d\n",ans);
}
cdoj 方师傅与01串(二叉树)
最新推荐文章于 2023-09-06 00:42:28 发布