码迷,mamicode.com
首页 > 其他好文 > 详细

字典树(模型体)

时间:2014-08-01 22:51:52      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:des   os   io   for   ar   div   new   amp   

给出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\n",ans);
     FREE(p);
  return 0;
}

字典树(模型体),布布扣,bubuko.com

字典树(模型体)

标签:des   os   io   for   ar   div   new   amp   

原文地址:http://www.cnblogs.com/yspworld/p/3885806.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!