2013级测试赛 -- A (字典树)

来源:互联网 发布:淘宝上的凯威淘淘通讯 编辑:程序博客网 时间:2024/05/16 17:26

A

Time Limit: 60MS Memory limit: 65536K

题目描述

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

输入

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

输出

输出一个数字代表答案。

示例输入

5abaabbwabaz

示例输出

2



一开始毫无思路,暴力指定超时,没有想到用字典树,要不是WJJ这题A不了。。



#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>using namespace std;const int N=26; int ma=0;struct node{    struct node *next [N];    int f;};struct node *creat (){    struct node *t;    t=(struct node *)malloc (sizeof (struct node));    for (int i=0; i<N; i++)        t->next[i]=NULL;    t->f=0;    return t;};void jia (struct node *t,char *a){    int n=strlen (a);    int ans ;    for (int i=0; i<n; i++)    {        ans = a[i]-'a';        if (t->next[ans]==NULL)            t->next[ans]=creat ();        t=t->next[ans];    }    t->f++;    if (t->f > ma)        ma=t->f;};int main (){    struct node *tree;    int n;    char a[10];    tree= creat ();    scanf ("%d",&n);    for (int i=0; i<n; i++)    {        scanf("%s",a);        jia (tree,a);    }    printf ("%d\n",ma);    return 0;}


0 0
原创粉丝点击