oj2892(字典树)

一改时间以后WA了,我就知道这题是考字典树,可惜代码怎么也不会敲了,郁闷。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Node
{
    int flag;
 struct Node *next[26];

}Node,*Tree;
char a[200010][6];
int n,m;
int sum=0;
void Creat(Tree &T)
{
    int i;
 T=(Tree)malloc(sizeof(Node));
 T->flag=0;
 for(i=0;i<26;i++)
  T->next[i]=NULL;
}
void insert(Tree &T,char *s)
{
   int l,i,t;
   Tree p=T;
   l=strlen(s);
   for(i=0;i<l;i++)
   {
   t=s[i]-'a';
  if(p->next[t]==NULL)
   Creat(p->next[t]);
     p=p->next[t];

   }
   p->flag++;
   if(sum<p->flag)
    sum=p->flag;

}
void Delete(Node *p)
{
    int i;
    for(i=0; i<26; i++)
    {
        if(p->next[i]!=NULL)
            Delete(p->next[i]);
    }
    free(p);

}

int main()
{
    int i;
 Tree T;
 while(scanf("%d",&n)!=EOF)
 {
       Creat(T);
    sum=0;
    for(i=0;i<n;i++)
    {
       scanf("%s",a[i]);
    insert(T,a[i]);
    }
    printf("%d\n",sum);
       Delete(T);

 }
 return 0;
}

 

 

 

posted @   人艰不拆_zmc  阅读(216)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示