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的更多相关文章

  1. hdu 1800 Flying to the Mars

    Flying to the Mars 题意:找出题给的最少的递增序列(严格递增)的个数,其中序列中每个数字不多于30位:序列长度不长于3000: input: 4 (n) 10 20 30 04 ou ...

  2. HDU 1800——Flying to the Mars——————【字符串哈希】

    Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  3. --hdu 1800 Flying to the Mars(贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1800 Ac code: #include<stdio.h> #include<std ...

  4. HDU - 1800 Flying to the Mars 【贪心】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1800 题意 给出N个人的 level 然后 高的level 的 人 是可以携带 比他低level 的人 ...

  5. HDU 1800 Flying to the Mars 字典树,STL中的map ,哈希树

    http://acm.hdu.edu.cn/showproblem.php?pid=1800 字典树 #include<iostream> #include<string.h> ...

  6. hdu 1800 Flying to the Mars(简单模拟,string,字符串)

    题目 又来了string的基本用法 //less than 30 digits //等级长度甚至是超过了int64,所以要用字符串来模拟,然后注意去掉前导零 //最多重复的个数就是答案 //关于str ...

  7. 杭电 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 ...

  8. 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 ...

  9. HDOJ.1800 Flying to the Mars(贪心+map)

    Flying to the Mars 点我挑战题目 题意分析 有n个人,每个人都有一定的等级,高等级的人可以教低等级的人骑扫帚,并且他们可以共用一个扫帚,问至少需要几个扫帚. 这道题与最少拦截系统有异 ...

随机推荐

  1. BOOT项目依赖另外一个模块的包,开发的时候都正常,执行clean package打包成jar时却提示依赖的模块包不存在。

    我在service1模块里依赖了common模块,开发的时候包都能正常引用到,启动也能正常测试访问,可是奇怪的是,当我要打包成jar包时,就提示service1里依赖common的包都不存在,之前从没 ...

  2. passwd---设置用户密码的相关信息

    passwd命令   passwd命令用于设置用户的认证信息,包括用户密码.密码过期时间等.系统管理者则能用它管理系统用户的密码.只有管理者可以指定用户名称,一般用户只能变更自己的密码. 语法 pas ...

  3. 【Uva 1633】Dyslexic Gollum

    [Link]: [Description] 输入正整数n和k(1≤n≤400,1≤k≤10),求长度为n的01串中有多少个不含长度至少 为k的回文连续子串.例如,n=k=3时只有4个串满足条件:001 ...

  4. HDU——T 3336 Count the string

    http://acm.hdu.edu.cn/showproblem.php?pid=3336 Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...

  5. [AngularFire] Angular File Uploads to Firebase Storage with Angular control value accessor

    The upload class will be used in the service layer. Notice it has a constructor for file attribute, ...

  6. 堆-heap

    #include <vector> #include <cstdio> using namespace std; class Heap { private : vector&l ...

  7. gullo.me 的 natvps

    gullo.me 的 natvps 1. 在下面的地址中输入内网 IP,查看许可的网络资源 https://hosting.gullo.me/plugin/support_manager/knowle ...

  8. 1.3 Quick Start中 Step 4: Send some messages官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Step 4: Send some messages Step : 发送消息 Kaf ...

  9. java匿名内部类使用场景列举

    示例一: package com;      interface Operation {       double operateTwoIntNum(int a, int b);   }      p ...

  10. 洛谷 P2026 求一次函数解析式

    P2026 求一次函数解析式 题目背景 做数学寒假作业的怨念…… 题目描述 给定两个整点的坐标,求它们所在直线的函数解析式(一次函数). 输入输出格式 输入格式: 输入共两行. 第一行有两个整数x1, ...