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个人,每个人都有一定的等级,高等级的人可以教低等级的人骑扫帚,并且他们可以共用一个扫帚,问至少需要几个扫帚. 这道题与最少拦截系统有异 ...
随机推荐
- 分享一个关于js原型链的理解
http://www.cnblogs.com/wyaocn/p/5815761.html
- Spring--之旅
spring的地位 如图可以看出,sping纵跨整个项目架构,它是一个容器框架.下面使用一个简单的项目来认识spring. 快速入门 step 1.新建一个普通Java工程,spring只是一种容器, ...
- 虚拟机上的Ubuntu开机显示“无法应用原保存的显示器配置”
如图: 解决方法: 删除monitors.xml 文件 rm ~/.config/monitors.xml
- Onsctl 配置ONS服务(10G)
Onsctl Onsctl这个命令是用来管理ONS(Oracle Notification Service)是OracleClustser实现FAN Event Push模型的基础. 在RAC环境下. ...
- 自定义控件学习——仿qq侧滑栏
效果 主要步骤: 1. 在xml布局里摆放内容. include 2. 在自定义ViewGroup里, 进行measure测量, layout布局 3. 响应用户的触摸事件 4. i ...
- POJ 1320 Street Numbers Pell方程
http://poj.org/problem?id=1320 题意很简单,有序列 1,2,3...(a-1),a,(a+1)...b 要使以a为分界的 前缀和 和 后缀和 相等 求a,b 因为序列很 ...
- C#监控代码运行的时间
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); //开始监视代码运行时间 ...
- vue的mode: 'history'模式
const router = new VueRouter({ mode: 'history', routes: [...] }) 不用mode: 'history'的时候,页面url地址后面会加上一个 ...
- CSRF Failed: Referer checking failed - no Referer
postman模拟登录出了这个错误,其实看标题就知道大概是怎么回事,网上大概找了办法,也没说到位,所以干脆自己找源码了. 问题很明显就是出在 CSRF 上,理所当然去查看 CsrfViewMiddle ...
- C#操作SQLite方法实例详解
用 C# 访问 SQLite 入门(1) CC++C#SQLiteFirefox 用 C# 访问 SQLite 入门 (1) SQLite 在 VS C# 环境下的开发,网上已经有很多教程.我也是从 ...