查找字符出现次数最多(再看)

来源:互联网 发布:下载定时开关机软件 编辑:程序博客网 时间:2024/06/05 16:18
Time Limit:60MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu
Submit Status Practice SDUTOJ 2892

Description

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

Input

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

Output

输出一个数字代表答案。

Sample Input

5abaabbwabaz

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;}

0 0