A(SDUT-OJ 2892)----字典树

A

Time Limit: 60ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

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

输入

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

输出

输出一个数字代表答案。

示例输入

5
aba
abb
w
aba
z

示例输出

2

提示

 

来源

 

示例程序

 
  • 提交
  • 状态
  • 讨论
    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <cstdio>
    #include <queue>
    #include <stack>
    #include <cmath>
    #include <map>
    
    using namespace std;
    
    //char s[100000][6];
    
    struct node
    {
        int ans;
        struct node *next[26];
    };
    
    struct  node *creat()
    {
        int i;
        struct  node *p;
        p=(struct  node *)malloc(sizeof(struct node ));
        for(i=0;i<26;i++)
        {
            p->next[i]=NULL;
        }
        p->ans=NULL;
        return p;
    }
    
    void Insert(struct node *&root,char *s,int &Max)
    {
        if(root==NULL)
        {
            root=creat();
        }
        if(s[0]!='\0')
        {
            Insert(root->next[s[0]-'a'],s+1,Max);
        }
        else
        {
            root->ans++;
            Max=max(root->ans,Max);
        }
    }
    
    int main()
    {
        struct node *root=NULL;
        int n,Max=1,i,j;
        char s[6];
        while(scanf("%d",&n)!=EOF)
        {
        for(i=0;i<n;i++)
        {
            scanf("%s",s);
            Insert(root,s,Max);
        }
        printf("%d\n",Max);
        }
        return 0;
    }
    
     
    
    
    
    /**************************************
    	Problem id	: SDUT OJ 2892 
    	User name	: mxjr130326刘继国 
    	Result		: Accepted 
    	Take Memory	: 4508K 
    	Take Time	: 20MS 
    	Submit Time	: 2014-06-28 21:38:58  
    **************************************/

    字典树是个很神奇的东西,建树,插入,查找,释放空间
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值