HDU 1800 Flying to the Mars Trie或者hash
http://acm.hdu.edu.cn/showproblem.php?pid=1800
题目大意:
又是废话连篇
给你一些由数字组成的字符串,判断去掉前导0后那个字符串出现频率最高。
一开始敲hash数组开大点嘛。。TLE,开小WA。。肯定是我hash函数写得不好。QAQ
然后我就直接上字典树了。。。
再然后此题的哈希我问了大牛他是直接存hash值我是存个数。。。然后我就牺牲了
然后我又敲了一遍,然后就AC了。
然后就没有然后了。
方法一:字典树Trie
#include<cstdio>
#include<cstring>
struct node
{
char c;
int cnt;
node *next[10];
node()
{
cnt=0;
for(int i=0;i<10;i++)
next[i]=NULL;
}
};
struct Trie
{
node * root;
int insert(char *s)
{
while(*s=='0')
s++;
int len=strlen(s);
node *cur=root;
for(int i=0;i<len;i++)
{
int id=s[i]-'0';
if(cur->next[id]==NULL)
{
node *temp=new node;
temp->c=s[i];
cur->next[id]=temp; }
cur=cur->next[id];
} return ++cur->cnt;
}
}trie; int main()
{
int n;
while(~scanf("%d",&n))
{
trie.root=new node;
char temp[32];
int ans=1;
for(int i=0;i<n;i++)
{
scanf("%s",temp);
int cnt=trie.insert(temp);
if(ans < cnt)
ans=cnt;
}
printf("%d\n",ans);
} return 0;
}
方法二:hash
关于hash函数选取,选择比给定的个数大的素数就可以了。要素数,才能减少冲突!
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int MAXN=3010;
LL hash_id[MAXN]; LL insert(char *s)
{
while(*s=='0')
s++; LL id=0;
for(;*s!='\0';s++)
id=id * 11 + *s-'0'; return id;
} int main()
{
int n;
while(~scanf("%d",&n))
{
char temp[32];
for(int i=0;i<n;i++)
{
scanf("%s",temp);
hash_id[i]=insert(temp);
}
sort(hash_id,hash_id+n);
int ans,cur;
ans=cur=1; for(int i=1;i<n;i++)
{
if(hash_id[i]==hash_id[i-1])
cur++;
else
{
cur=1;
}
if(cur > ans)
ans=cur;
} printf("%d\n",ans);
} return 0;
}
HDU 1800 Flying to the Mars Trie或者hash的更多相关文章
- hdu 1800 Flying to the Mars
Flying to the Mars 题意:找出题给的最少的递增序列(严格递增)的个数,其中序列中每个数字不多于30位:序列长度不长于3000: input: 4 (n) 10 20 30 04 ou ...
- HDU 1800——Flying to the Mars——————【字符串哈希】
Flying to the Mars Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- --hdu 1800 Flying to the Mars(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1800 Ac code: #include<stdio.h> #include<std ...
- HDU - 1800 Flying to the Mars 【贪心】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1800 题意 给出N个人的 level 然后 高的level 的 人 是可以携带 比他低level 的人 ...
- HDU 1800 Flying to the Mars 字典树,STL中的map ,哈希树
http://acm.hdu.edu.cn/showproblem.php?pid=1800 字典树 #include<iostream> #include<string.h> ...
- hdu 1800 Flying to the Mars(简单模拟,string,字符串)
题目 又来了string的基本用法 //less than 30 digits //等级长度甚至是超过了int64,所以要用字符串来模拟,然后注意去掉前导零 //最多重复的个数就是答案 //关于str ...
- 杭电 1800 Flying to the Mars(贪心)
http://acm.hdu.edu.cn/showproblem.php?pid=1800 Flying to the Mars Time Limit: 5000/1000 MS (Java/Oth ...
- hdu---(1800)Flying to the Mars(trie树)
Flying to the Mars Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDOJ.1800 Flying to the Mars(贪心+map)
Flying to the Mars 点我挑战题目 题意分析 有n个人,每个人都有一定的等级,高等级的人可以教低等级的人骑扫帚,并且他们可以共用一个扫帚,问至少需要几个扫帚. 这道题与最少拦截系统有异 ...
随机推荐
- Fragment-Transaction 源码分析
概述 这篇文章的简要分析了Activity中的Transaction和add,replace等操作以及backstack的工作原理. 分析transaction源码的原因是因为我在写一个测试代码的时候 ...
- GridDataView实现 点击任意一格可以修改
直接上代码好了 private void dgv1Member_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { string ...
- 深入了解"网上邻居"原理
说到“网上邻居”,相信很多人都很熟悉.但是说起“网上邻居”的工作机制,可能大家就不太清楚了. 要说“网上邻居”的工作机制,不妨联系一下生活中的例子:比如我(A),要拜访一个远方的朋友(B),我要去他的 ...
- deep-in-es6(七)
Symbols对象 JavaScript的第七种原始类型 以前的数据类型: Undefined 未定义 Null 空值 Boolean 布尔类型 Number 数字类型 String 字符串类型 Ob ...
- deep-in-es6(四)
不定参数和默认参数: function containsAll(str) { for(var i = 1;i < arguments.length;i++) { var hasStr = arg ...
- linux 查找文件和文件夹与下载命令
查找命令: 查找根目录下查找文件夹名称叫dir的目录地址 find / -name dir -d 查找/var/www/目录下叫index.jsp的文件 find /var/www/ -name i ...
- Java开发者最经常使用19个Linux命令
1.查找文件 find / -name filename.txt 依据名称查找/文件夹下的filename.txt文件. 2.查看一个程序是否执行 ps –ef|grep tomcat 查看全部有关t ...
- 图片裁剪的js有哪些(整理)
图片裁剪的js有哪些(整理) 一.总结 一句话总结:如果用了amaze框架就去amaze框架的插件库里面找图片裁剪插件,如果没用,jcrop和cropper都不错. 1.amazeui的插件库中有很多 ...
- dom4j解析xml文件和字符串
转自:http://www.cnblogs.com/black-spike/p/9776180.html 最近在工作中,需要调别的接口,接口返回的是一个字符串,而且内容是xml格式的,结果在解析jso ...
- POJ3622 Gourmet Grazers(FHQ Treap)
Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 2363 Accepted: 881 Description Like s ...