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个人,每个人都有一定的等级,高等级的人可以教低等级的人骑扫帚,并且他们可以共用一个扫帚,问至少需要几个扫帚. 这道题与最少拦截系统有异 ...
随机推荐
- Codefroces Educational Round 27 (A,B,C,D)
A. Chess Tourney time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- kali之Nmap (Network Mapper(网络映射器)
Nmap是主机扫描工具,他的图形化界面是Zenmap,分布式框架为Dnamp. Nmap可以完成以下任务: 主机探测 端口扫描 版本检测 系统检测 支持探测脚本的编写 Nmap在实际中应用场合如下: ...
- 各种join一目了然: join 、inner join、left join 、right join、full join
各种join一幅图一目了然 一下每幅图都是指: A * join B on A.id = B.in 这个帖子也非常形象.比較好:http://www.phpddt.com/db/inner_join- ...
- 在带(继承)TextView的控件中,在代码中动态更改TextView的文字颜色
今天由于公司项目需求,须要实现一种类似tab的选项卡,当时直接想到的就是使用RadioGroup和RadioButton来实现. 这种方法全然没问题.可是在后来的开发过程中,却遇到了一些困扰非常久的小 ...
- jquery--new返回值
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- BZOJ 1009 GT考试 (AC自动机 + 矩阵乘法加速dp)
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1009 题意: 准考证号为\(n\)位数\(X_1X_2....X_n(0<=X_ ...
- 洛谷 P1626 象棋比赛
P1626 象棋比赛 题目描述 有N个人要参加国际象棋比赛,该比赛要进行K场对弈.每个人最多参加两场对弈,最少参加零场对弈.每个人都有一个与其他人不相同的等级(用一个正整数来表示). 在对弈中,等级高 ...
- CF #261 div2 D. Pashmak and Parmida's problem (树状数组版)
Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants he ...
- linux下多进程的文件拷贝与进程相关的一些基础知识
之前实现了用文件IO的方式能够实现文件的拷贝,那么对于进程而言,我们是否也能够实现呢? 答案是肯定的. 进程资源: 首先我们先回想一下,进程的执行须要哪些资源呢?其资源包含CPU资源,内存资源,当然还 ...
- java8新增特性(二)----函数式接口(Functional)
上一篇博客介绍了java8新增的Lambda表达式,这一节介绍一下java8的函数式编程,两者之间有什么联系呢?请往下看~~~ Lambda表达式怎样在java类型中表示的呢? 语言设计者投入了大量的 ...