Flying to the Mars

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

思路:将节目转化为相同数的最多个数即可~~
这时就随便怎么搞了。我是直接用了map(开始不会hash啊…)来找mx;
但是和字符串hash相比,时间性能不好。
之后看了ACdreamers,学习了ELFhash之后发现挺好用的~~下面将讲解一下我对ELFhash的理解。

  1. // 702ms 1808k
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. #define rep(i,n) for(int i = 0;i < (n);i++)
  5. map<string,int> mp;
  6. int main()
  7. {
  8. ios_base::sync_with_stdio(false);cin.tie();cout.tie();cerr<<"";
  9. int n,i;
  10. while(cin>>n){
  11. mp.clear();
  12. string str;
  13. rep(i,n){
  14. cin>>str;
  15. int j = ;
  16. for(;j < str.size();j++)if(str[j] != '') break;
  17. mp[str.substr(j)]++;
  18. }
  19. int ans = ;
  20. for(auto m : mp){
  21. ans = max(ans , m.second);
  22. }
  23. cout<<ans<<endl;
  24. }
  25. }

ELFhash:
hash要对字符串里面的每一个字符进行运算,之后得到一个”相对”不同的值(因为有冲突);字符有八个字节的,但是里面对高位的四个字节与前一个字符的低的四个字节相加了,低的四个字节填补了h左移空出来的四个字节(不知理解得是否有误,若有误,请指正~)并且里面的位是从0开始的,即当h<<4时开始的0位,现在就是第4位了。里面的hash运算还有就是在h要满的时候(即h的高四位28~31不为0时),下一次再加进字符时,就会直接移走了,但是算法要将这高四位和4~8位的数再做一次XOR运算,应该是使得得到的hash更随机~~(里面0xF0000000L就表示28~31位全是1,其余位为0)

hash::code @ACdreamers

  1. 499ms
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. #define rep(i,n) for(int i = 0;i < (n);i++)
  5. #define MS0(a) memset(a,0,sizeof(a))
  6. const int MOD = ;
  7. int Hash[MOD],cnt[MOD];
  8. unsigned int ELFhash(char *str)//unsigned
  9. {
  10. unsigned int h = ;
  11. unsigned int x;
  12. while(*str){
  13. h = (h << ) + *str++;//与前一个字符的第四位字节相加;
  14. x = h & 0xF0000000L;//x为h的28~31位
  15. if(x){
  16. h ^= x>>;  //高四位和4~8位的数再做一次XOR运算,不然没运算就移走了,可惜~
  17. h &= ~x;  // h清空28~31位(运算完了)
  18. }
  19. }
  20. return h & 0x7FFFFFFF;//返回unsigned,即0x7FFFFFFF为unsigned的二进制全为1
  21. }
  22. int Hashhit(char *str)
  23. {
  24. while(*str == '') str++;
  25. int k = ELFhash(str);
  26. int t = k % MOD;
  27. while(Hash[t] && Hash[t] != k) //解决冲突;
  28. t = (t + )%MOD;
  29. if(Hash[t] == )
  30. Hash[t] = k;
  31. return ++cnt[t];
  32. }
  33. int main()
  34. {
  35. int n;char str[];
  36. while(scanf("%d",&n) == ){
  37. MS0(Hash);MS0(cnt);
  38. int ans = ;
  39. rep(i,n){
  40. scanf("%s",str);
  41. ans = max(ans,Hashhit(str));
  42. }
  43. printf("%d\n",ans);
  44. }
  45. }

hdu 1800 Flying to the Mars的更多相关文章

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

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

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

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

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

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

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

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

  5. HDU 1800 Flying to the Mars Trie或者hash

    http://acm.hdu.edu.cn/showproblem.php?pid=1800 题目大意: 又是废话连篇 给你一些由数字组成的字符串,判断去掉前导0后那个字符串出现频率最高. 一开始敲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. HDOJ.1800 Flying to the Mars(贪心+map)

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

  9. HDOJ 1800 Flying to the Mars 盲目搜索......................so easy...........

    check the original problem here:http://acm.hdu.edu.cn/showproblem.php?pid=1800 the AC code: #include ...

随机推荐

  1. ThinkPHP3.1快速入门(2)数据CURD

    上一篇中,我们了解了ThinkPHP的基础部分,以及如何创建一个控制器和模板,并知道了M方法的用法,本篇将会讲解下数据的CURD操作,探索下更多的数据操作. CURD CURD是一个数据库技术中的缩写 ...

  2. android105 jni概念

    JNI(Java Native Interface,JAVA原生接口) ,通过JNIjava代码可以调用C代码,JNI在安卓中用的很多.安卓中的框架层就是用过JNI访问类库层的.Iphone是用C/C ...

  3. 实现类似shared_ptr的引用计数

    13.27 定义使用引用计数版本的HasPtr #include<iostream> #include<string> #include<new> using na ...

  4. UIStackView 看我就够了

    介绍 UIStackView 是 iOS9新增的一个布局技术.熟练掌握相当节省布局时间. UIStackView 是 UIView 的子类,是用来约束子控件的一个控件.但他的作用仅限于此,他不能用来呈 ...

  5. Java基础知识强化之集合框架笔记69:Collections类之ArrayList存储自自定义对象并排序的案例

    1. ArrayList存储自自定义对象并排序的案例: ArrayList存储自自定义对象,并使用Collections对ArrayList存储基本包装类的元素排序. 2. 代码实现: (1)Stud ...

  6. iOS CocoaPods自动管理第三方开源库

    最近在开发中发现在项目中使用了好多第三方库,然而第三方更新的时候本地却不能及时更新.然而CocoaPods则可以管理第三方依赖包的更新,这些“体力活”会被节省好多时间,下面介绍一下CocoaPods的 ...

  7. File类最基础知识

    package File; /** * 创建一个文件: * 判断是否存在,若存在,则创建,若不存在,则删除,最后输出文件是否存在. */ import java.io.File; import jav ...

  8. JDK版本过高,导致Eclipse报错

    1.JDK版本如果比较高,而使用的eclipse版本比较低,导致在eclispe中不能识别而报错.   2.点击Attach Source添加rt.jar后,又出现如下错误 3.这样的错误就是由于ec ...

  9. Entity Framework 使用sql语句分页(查询单表)

    1.查询单表 var pageSize = 2;//条数 var pageIndex = 2;//索引 var sql = @" SELECT D.* FROM ( SELECT ROW_N ...

  10. 网站部署后,ie不能显示本地的图片

    html:<div id="imgPreview"  style='width:144px; height:80px;'>                        ...