zoukankan      html  css  js  c++  java
  • 字典树(模型体)

    给出n(1<= n && n <= 2*10^6)个字符串,每个字符串只包含小写英文字母,且最多有五个。问这n个字符串中出现次数最多的有多少个。

    输入

    单组输入。第一行输入一个数字n,接下来n行,每行包含一个字符串。

    输出

    输出一个数字代表答案。

    示例输入

    5
    aba
    abb
    w
    aba
    z

    示例输出

    2

    提示

     

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    const int N = 20010;
    using namespace std;

    struct node
    {
      int flag;
      node *next[26];
    };

    int n,m,ans = 0;

    struct node *Creat()
    {
      node *p = new node;
      for(int i=0; i<26; i++ )
      {
        p->next[i] = NULL;
      }
      p->flag = 0;
      return p;
    }


    void INsert( node *root, char *b)
    {
      int num;
      int len = strlen(b);

      node *p = root;

      for(int i = 0;i<len;i++)
      {
        num = b[i]-'a';
         if(p->next[num]==NULL)
         {
           p->next[num] = Creat();
         }
         p = p->next[num];

      }
      p->flag++;
      if(p->flag > ans)
        ans = p->flag;
    }

    void FREE(struct node*root)
    {
      for(int i = 0;i<n;i++)
      {
        if(root->next[i]!=NULL)
        {
          FREE(root->next[i]);
        }
      }
      free(root);
    }

    int main()
    {
      char a[N][50],s[50];
      node *p;

         scanf("%d",&m);
         p = Creat();


         for(int i = 0;i<m;i++)
         {
           scanf("%s",s);
           INsert(p,s);
         }
         printf("%d ",ans);
         FREE(p);
      return 0;
    }

  • 相关阅读:
    中国剩余定理
    Codeforces 240 F. TorCoder
    ZOJ 1202 Divide and Count
    洛谷 3380 【模板】二逼平衡树(树状数组套权值线段树)
    bzoj 3196/ Tyvj 1730 二逼平衡树 (线段树套平衡树)
    uva 10791
    uva 699 The Falling Leaves
    uva 839 Not so Mobile
    2017 济南综合班 Day 5
    2017 济南综合班 Day 4
  • 原文地址:https://www.cnblogs.com/yspworld/p/3885806.html
Copyright © 2011-2022 走看看