zoukankan      html  css  js  c++  java
  • 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 ",sum);
           Delete(T);

     }
     return 0;
    }

  • 相关阅读:
    VS.net 2005快捷键一览表
    POJ 1141 Brackets Sequence
    POJ 3264 Balanced Lineup RMQ问题的ST解法
    Hdu 4267 A Simple Problem with Integers
    hdu 4009 Transfer water
    HDU 4288 Coder
    POJ 1679 The Unique MST
    hdu 4291 A Short problem
    hdu 1175 连连看(DFS)
    POJ 3164 Command Network
  • 原文地址:https://www.cnblogs.com/zhangmingcheng/p/3804890.html
Copyright © 2011-2022 走看看