本文出自:http://blog.csdn.net/dr5459

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4712

题目意思:

海明距离:任意两个树异或后二进制含1的个数

要你求出最小的海明距离

解题思路:

因为数的格式是固定的,所以可以预处理16进制中任意两个数的异或1的个数

这样在求的时候,可以在O(5)内求出

至于怎么去求所有的,看的群里的思路,随机10W次,而且要保证随机的两个数不同,我随机了6W次

然后就A了,就A了,真的被吓到了,下面上代码:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<ctime>
  5. #include<cstdlib>
  6. using namespace std;
  7.  
  8. int cmp[16][16];
  9.  
  10. char data[1000005][6];
  11.  
  12. int fun(int q,int w)
  13. {
  14. int a,b;
  15. int ret = 0;
  16. for(int i=0;i<5;i++)
  17. {
  18. char x = data[q][i];
  19. char y = data[w][i];
  20. if(x>='0' && x<='9')
  21. a=x-'0';
  22. else
  23. a=x-'A'+10;
  24.  
  25. if(y>='0' && y<='9')
  26. b=y-'0';
  27. else
  28. b=y-'A'+10;
  29.  
  30. ret += cmp[a][b];
  31. }
  32. return ret;
  33. }
  34.  
  35. int main()
  36. {
  37. for(int i=0;i<=15;i++)
  38. {
  39. for(int j=0;j<=15;j++)
  40. {
  41. int ans=0;
  42. int tmp = i^j;
  43. for(int k=0;k<4;k++)
  44. if((1<<k) & tmp)
  45. ans++;
  46. //cout<<i<<" "<<j<<" "<<ans<<endl;
  47. cmp[i][j] = ans;
  48. }
  49. }
  50.  
  51. int n;
  52. int t;
  53. scanf("%d",&t);
  54. while(t--)
  55. {
  56. scanf("%d",&n);
  57. for(int i=1;i<=n;i++)
  58. scanf("%s",data[i]);
  59.  
  60. int ans = 0x3f3f3f3f;
  61. srand( (unsigned)time( NULL ) );
  62. for(int i=1;i<=60000;i++)
  63. {
  64. int a = rand()%n+1;
  65. int b = rand()%n+1;
  66. if(a==b)
  67. b = b%n+1;
  68. int tmp = fun(a,b);
  69. if(tmp < ans)
  70. ans = tmp;
  71. }
  72.  
  73. cout<<ans<<endl;
  74. }
  75.  
  76. return 0;
  77. }

HDU4712-----Hamming Distance------超级大水题的更多相关文章

  1. hdu4712 Hamming Distance

    Hamming Distance Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) To ...

  2. HDU4712 Hamming Distance (随机化)

    link:http://acm.hdu.edu.cn/showproblem.php?pid=4712 题意:给1e5个数字,输出这些数中,最小的海明码距离. 思路:距离的范围是0到20.而且每个数的 ...

  3. hdu 1228 A+B 字符串处理 超级大水题

    中文意思不解释. 很水,我本来想用switch处理字符串,然后编译不通过...原来switch只能处理整数型的啊,我都忘了. 然后就有了很挫的一大串if代码了... 代码: #include < ...

  4. hdu 1229 超级大水题

      Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Desc ...

  5. hdu 4548 美素数 超级大水题

    美素数 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submis ...

  6. Lweb and String 超级大水题

    Lweb and String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  7. Total Hamming Distance

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  8. 461. Hamming Distance and 477. Total Hamming Distance in Python

    题目: The Hamming distance between two integers is the number of positions at which the corresponding ...

  9. LeetCode Total Hamming Distance

    原题链接在这里:https://leetcode.com/problems/total-hamming-distance/ 题目: The Hamming distance between two i ...

  10. LeetCode Hamming Distance

    原题链接在这里:https://leetcode.com/problems/hamming-distance/ 题目: The Hamming distance between two integer ...

随机推荐

  1. Python 数据处理扩展包: numpy 和 pandas 模块介绍

    一.numpy模块 NumPy(Numeric Python)模块是Python的一种开源的数值计算扩展.这种工具可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list str ...

  2. 转:Bootstrap研究 精巧的网格布局系统

    本网格布局系统属于Scaffolding(框架,布局)部分.在Scaffolding里面有(固定)网格布局(Grid System)和流式网格布局(Fluid Grid System).本文讨论第一种 ...

  3. 15个最受欢迎的Python开源框架

    以下是伯乐在线从GitHub中整理出的15个最受欢迎的Python开源框架.这些框架包括事件I/O,OLAP,Web开发,高性能网络通信,测试,爬虫等. Django: Python Web应用开发框 ...

  4. oschina开源硬件其它开源,开源硬件

    硬件驱动/工具 25Linux内核 101桌面环境 40开源字体 58嵌入式操作系统 33输入法 110开源硬件 57开源图书 5开源家居 17物联网 87开源货币/比特币 32NASA 开源项目 1 ...

  5. 基于visual Studio2013解决算法导论之020单链表

     题目 单链表操作 解决代码及点评 #include <iostream> using namespace std; struct LinkNode { public: LinkNo ...

  6. Python字典处理技巧

    从字典中取值(不想由于搜索的键不存在而异常) 解决方法: 使用字典的get方法 (get方法只读取字典的值而不会去修改字典) d={'key':'value'} print d.get('key',' ...

  7. NGUI出现Shader wants normals, but the mesh UIAtlas doesn&#39;t have them

    NGUI出现Shader wants normals, but the mesh UIAtlas doesn't have them,没有网格法线,打开UI Root上 UIPanel组建上的 Nor ...

  8. PHP - 对象转json - json转数组

    前台js转为json,传给php后台,php后台接收并转为数组. 效果: -- 前台js将对象转为json: var rows = JSON.stringify(rows); 后台php接收转为数组: ...

  9. [转]Centos 6.5 优化 一些基础优化和安全设置

    关于CentOS服务器的优化下文作为参考. 本文 centos 6.5 优化 的项有18处: 1.centos6.5最小化安装后启动网卡2.ifconfig查询IP进行SSH链接3.更新系统源并且升级 ...

  10. SuperSocket源码解析之开篇

    一 简介 官方介绍:SuperSocket 是一个轻量级, 跨平台而且可扩展的 .Net/Mono Socket 服务器程序框架.你无须了解如何使用 Socket, 如何维护 Socket 连接和 S ...