HDU 1800 Flying to the Mars

原题目链接HDU1800


分类

HDU 字典树


题意

求出数字出现最多的次数。当n=0时,输出1.

样例输入输出

Sample Input

4
10
20
30
04
5
2
3
4
3
4

Sample Output

1
2

想法

(字典树,涉及到高级数据结构)
每一个数字最长30位,用longlong也没用,所以要用字符串存储,自然想到了字典树,当然还有其他解法,map(时间勉强过),哈希(不会)
注意:题目中说000123和123是一个数字,即要除去前导的0,稍作处理即可


代码

280ms

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define maxn 11111
using namespace std;
struct Trie{
	int ch[maxn][10];
	int val[maxn];
	int sz;//结点个数
	Trie(){
		sz = 1;
		memset(ch[0],0,sizeof(ch[0]));
		memset(val,0,sizeof(val));
	} 
	int idx(char c){
		return c - '0';
	}
	
	int insert(char* s){
		int u=0 ,  n= strlen(s);
		int j=0;
		while(s[j]=='0')j++;
		for(int i=j;i<n;i++){
			int c = idx(s[i]);
			if(!ch[u][c])//结点不存在
			{
				memset(ch[sz],0,sizeof(ch[sz]));
				ch[u][c] = sz++;
			}
			u = ch[u][c];
		}
		val[u]++;
		return val[u];
	}
};

int main()
{
	int n,mmax;
	char str[44];
	while(~scanf("%d",&n)){
		Trie T;
		mmax = 0;
		while(n--){
			scanf("%s",str);
			int t = T.insert(str);
			mmax= max(mmax,t);
		}
		if(mmax == 0) 
			mmax = 1;
		printf("%d\n",mmax);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值