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

A

Time Limit: 60MS Memory limit: 65536K

题目描述

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

输入

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

输出

输出一个数字代表答案。

示例输入

5
aba
abb
w
aba
z

示例输出

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值