查找字符出現次數最多(再看)

Time Limit:60MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Description

給出n(1<= n && n <= 2*10^6)個字符串,每個字符串只包含小寫英文字母,且最多有五個。問這n個字符串中出現次數最多的有多少個。

Input

單組輸入。第一行輸入一個數字n,接下來n行,每行包含一個字符串。

Output

輸出一個數字代表答案。

Sample Input

5
aba
abb
w
aba
z

Sample Output

2

Hint

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>


using namespace std;

struct node{
  int flag;
  node *next[26];
};
int n,m;
int Max=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 *tree,char *b)
{
  int ans;
  int len = strlen(b);
  node *p = tree;
  for(int i = 0;i<len;i++)
  {
    ans = b[i]-'a';
     if(p->next[ans]==NULL)
     {
       p->next[ans] = creat();
     }
    p = p->next[ans];
  }
  p->flag++;
  if((p->flag) >Max)
    Max = p->flag;
}

void freenode( node*tree)
{
  for(int i = 0;i<n;i++)
  {
    if(tree->next[i]!=NULL)
    {
      freenode(tree->next[i]);
    }
  }
  free(tree);
}
int main()
{
  char a[2000000][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",Max);
     freenode(p);
  return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章