HDU4712-----Hamming Distance------超级大水题
本文出自:http://blog.csdn.net/dr5459
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4712
题目意思:
海明距离:任意两个树异或后二进制含1的个数
要你求出最小的海明距离
解题思路:
因为数的格式是固定的,所以可以预处理16进制中任意两个数的异或1的个数
这样在求的时候,可以在O(5)内求出
至于怎么去求所有的,看的群里的思路,随机10W次,而且要保证随机的两个数不同,我随机了6W次
然后就A了,就A了,真的被吓到了,下面上代码:
- #include<cstdio>
- #include<cstring>
- #include<iostream>
- #include<ctime>
- #include<cstdlib>
- using namespace std;
- int cmp[16][16];
- char data[1000005][6];
- int fun(int q,int w)
- {
- int a,b;
- int ret = 0;
- for(int i=0;i<5;i++)
- {
- char x = data[q][i];
- char y = data[w][i];
- if(x>='0' && x<='9')
- a=x-'0';
- else
- a=x-'A'+10;
- if(y>='0' && y<='9')
- b=y-'0';
- else
- b=y-'A'+10;
- ret += cmp[a][b];
- }
- return ret;
- }
- int main()
- {
- for(int i=0;i<=15;i++)
- {
- for(int j=0;j<=15;j++)
- {
- int ans=0;
- int tmp = i^j;
- for(int k=0;k<4;k++)
- if((1<<k) & tmp)
- ans++;
- //cout<<i<<" "<<j<<" "<<ans<<endl;
- cmp[i][j] = ans;
- }
- }
- int n;
- int t;
- scanf("%d",&t);
- while(t--)
- {
- scanf("%d",&n);
- for(int i=1;i<=n;i++)
- scanf("%s",data[i]);
- int ans = 0x3f3f3f3f;
- srand( (unsigned)time( NULL ) );
- for(int i=1;i<=60000;i++)
- {
- int a = rand()%n+1;
- int b = rand()%n+1;
- if(a==b)
- b = b%n+1;
- int tmp = fun(a,b);
- if(tmp < ans)
- ans = tmp;
- }
- cout<<ans<<endl;
- }
- return 0;
- }
HDU4712-----Hamming Distance------超级大水题的更多相关文章
- hdu4712 Hamming Distance
Hamming Distance Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) To ...
- HDU4712 Hamming Distance (随机化)
link:http://acm.hdu.edu.cn/showproblem.php?pid=4712 题意:给1e5个数字,输出这些数中,最小的海明码距离. 思路:距离的范围是0到20.而且每个数的 ...
- hdu 1228 A+B 字符串处理 超级大水题
中文意思不解释. 很水,我本来想用switch处理字符串,然后编译不通过...原来switch只能处理整数型的啊,我都忘了. 然后就有了很挫的一大串if代码了... 代码: #include < ...
- hdu 1229 超级大水题
Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status Desc ...
- hdu 4548 美素数 超级大水题
美素数 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Submis ...
- Lweb and String 超级大水题
Lweb and String Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- Total Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- 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 ...
- LeetCode Total Hamming Distance
原题链接在这里:https://leetcode.com/problems/total-hamming-distance/ 题目: The Hamming distance between two i ...
- LeetCode Hamming Distance
原题链接在这里:https://leetcode.com/problems/hamming-distance/ 题目: The Hamming distance between two integer ...
随机推荐
- Python 数据处理扩展包: numpy 和 pandas 模块介绍
一.numpy模块 NumPy(Numeric Python)模块是Python的一种开源的数值计算扩展.这种工具可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list str ...
- 转:Bootstrap研究 精巧的网格布局系统
本网格布局系统属于Scaffolding(框架,布局)部分.在Scaffolding里面有(固定)网格布局(Grid System)和流式网格布局(Fluid Grid System).本文讨论第一种 ...
- 15个最受欢迎的Python开源框架
以下是伯乐在线从GitHub中整理出的15个最受欢迎的Python开源框架.这些框架包括事件I/O,OLAP,Web开发,高性能网络通信,测试,爬虫等. Django: Python Web应用开发框 ...
- oschina开源硬件其它开源,开源硬件
硬件驱动/工具 25Linux内核 101桌面环境 40开源字体 58嵌入式操作系统 33输入法 110开源硬件 57开源图书 5开源家居 17物联网 87开源货币/比特币 32NASA 开源项目 1 ...
- 基于visual Studio2013解决算法导论之020单链表
题目 单链表操作 解决代码及点评 #include <iostream> using namespace std; struct LinkNode { public: LinkNo ...
- Python字典处理技巧
从字典中取值(不想由于搜索的键不存在而异常) 解决方法: 使用字典的get方法 (get方法只读取字典的值而不会去修改字典) d={'key':'value'} print d.get('key',' ...
- NGUI出现Shader wants normals, but the mesh UIAtlas doesn't have them
NGUI出现Shader wants normals, but the mesh UIAtlas doesn't have them,没有网格法线,打开UI Root上 UIPanel组建上的 Nor ...
- PHP - 对象转json - json转数组
前台js转为json,传给php后台,php后台接收并转为数组. 效果: -- 前台js将对象转为json: var rows = JSON.stringify(rows); 后台php接收转为数组: ...
- [转]Centos 6.5 优化 一些基础优化和安全设置
关于CentOS服务器的优化下文作为参考. 本文 centos 6.5 优化 的项有18处: 1.centos6.5最小化安装后启动网卡2.ifconfig查询IP进行SSH链接3.更新系统源并且升级 ...
- SuperSocket源码解析之开篇
一 简介 官方介绍:SuperSocket 是一个轻量级, 跨平台而且可扩展的 .Net/Mono Socket 服务器程序框架.你无须了解如何使用 Socket, 如何维护 Socket 连接和 S ...