随机算法

求n个20位的2进制串的MinDist。

Dist:两个串的异或结果中1的个数

/*
随机算法
*/
#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<queue>
#include<stack>
#include<time.h>
#include<map>
#include<set>
using namespace std;
typedef long long int64;
//typedef __int64 int64;
typedef pair<int64,int64> PII;
#define MP(a,b) make_pair((a),(b))
const int inf = 0x3f3f3f3f;
const double pi=acos(-1.0);
const int dx[]={1,-1,0,0};
const int dy[]={0,0,1,-1};
const double eps = 1e-8;
const int maxm = 100000+10;
const int maxn = 105; char str[ maxm ][ 6 ];
int num1[ 22 ],num2[ 22 ]; int GetId( char aim ){
int ans ;
if( aim>='0'&&aim<='9' ) ans = aim - '0';
else if( aim>='A'&&aim<='Z' ) ans = aim - 'A' + 10;
return ans;
} int solve( int x,int y ){
int cnt = 0;
int cc ;
for( int i=0;i<5;i++ ){
int aim = GetId( str[x][i] );
cc = 4;
while( cc ){
num1[ cnt++ ] = aim%2;
aim /= 2;
cc -- ;
}
}
//printf("cnt = %d\n",cnt);
cnt = 0;
for( int i=0;i<5;i++ ){
int aim = GetId( str[y][i] );
cc = 4;
while( cc ){
num2[ cnt++ ] = aim%2;
aim /= 2;
cc -- ;
}
}
//printf("cnt = %d\n",cnt);
cnt = 0;
for( int i=0;i<20;i++ ){
if( num1[i]!=num2[i] )
cnt ++ ;
}
return cnt ;
} int main(){
int T;
scanf("%d",&T);
while( T-- ){
int n;
scanf("%d",&n);
for( int i=0;i<n;i++ )
scanf("%s",str[i]);
srand( time(NULL) );
int ans = inf;
int x,y;
for( int i=0;i<180000;i++ ){
x = rand()%n;
y = rand()%n;
if( x==y ) continue;
int temp = solve( x,y );
if( temp<ans )
ans = temp;
}
printf("%d\n",ans);
}
return 0;
}

HDU4712+随机算法的更多相关文章

  1. 微信红包中使用的技术:AA收款+随机算法

    除夕夜你领到红包了吗?有的说“我领了好几K!”“我领了几W!” 土豪何其多,苦逼也不少!有的说“我出来工作了,没压岁钱了,还要发红包”.那您有去抢微信红包吗?微信群中抢“新年红包”春节爆红.618微信 ...

  2. POJ 3318 Matrix Multiplication(随机算法)

    题目链接 随机算法使劲水...srand((unsigned)time(0))比srand(NULL)靠谱很多,可能是更加随机. #include <cstdio> #include &l ...

  3. 抽奖随机算法的技术探讨与C#实现

    一.模拟客户需求 1.1 客户A需求:要求每次都按照下图的概率随机,数量不限,每个用户只能抽一次,抽奖结果的分布与抽奖概率近似. 1.2 客户B需求:固定奖项10个,抽奖次数不限,每个用户只能抽一次, ...

  4. hdu 4712 (随机算法)

    第一次听说随机算法,在给的n组数据间随机取两个组比较,当随机次数达到一定量时,答案就出来了. #include<stdio.h> #include<stdlib.h> #inc ...

  5. 权重随机算法的java实现

    一.概述 平时,经常会遇到权重随机算法,从不同权重的N个元素中随机选择一个,并使得总体选择结果是按照权重分布的.如广告投放.负载均衡等. 如有4个元素A.B.C.D,权重分别为1.2.3.4,随机结果 ...

  6. hdu 4712 Hamming Distance ( 随机算法混过了 )

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

  7. LOJ #2540. 「PKUWC 2018」随机算法(概率dp)

    题意 LOJ #2540. 「PKUWC 2018」随机算法 题解 朴素的就是 \(O(n3^n)\) dp 写了一下有 \(50pts\) ... 大概就是每个点有三个状态 , 考虑了但不在独立集中 ...

  8. java 权重随机算法实现

    import java.util.*; /** * 权重随机算法实现 * a b c d 对应权重范围 --- [0,1).[1,3).[3,6).[6,10) */ public class Ran ...

  9. HDU 4712 Hamming Distance(随机算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4712 解题报告:输入n个数,用十六进制的方式输入的,任意选择其中的两个数进行异或,求异或后的数用二进制 ...

随机推荐

  1. Twemproxy 分布式集群缓存代理服务器

    Twemproxy 分布式集群缓存代理服务器 是一个使用C语言编写.以代理的方式实现的.轻量级的Redis代理服务器, 它通过引入一个代理层,将应用程序后端的多台Redis实例进行统一管理, 使 应用 ...

  2. CI(-)框架结构

    一 CI 是什么 CodeIgniter is an Application Development Framework - a toolkit - for people who build web ...

  3. js判断某个方法是否存在

    window.onload = function(){ try{ if(test && typeof(test) == "function"){ test(); } ...

  4. IOS 特定于设备的开发:获取和使用设备姿势(通过手机方向控制3d物体显示)

    利用设备的机载陀螺仪可以实现,当你旋转手机屏幕时,里面的画面不会随着视图更新而移动,以平衡物理运动. 下面例子利用少量简单的几何变换执行该操作.他建立一个运动管理器,订阅设备运动更新,然后基于运动管理 ...

  5. ODI学习笔记2--ODI产品架构

    ODI学习笔记2--ODI产品架构 ODI产品架构: ODI提供了以下几种管理工具:Designer 用于定义数据转换逻辑,这是最常用的开发工具,大部分的开发任务,包括data store的定义,in ...

  6. tomcat部署不正确

    我把一个名为cp的web项目导入myeclipse中,由于要再次导入该工程的新版本,我就把旧版本的cp项目改名成cp3.但是在运行新项目cp的时候,tomcat出问题,一直是之前旧cp对应的tomca ...

  7. css 自适应布局

    转载一篇文章: 自适应网页设计(Responsive Web Design) 作者: 阮一峰 移动设备正超过桌面设备,成为访问互联网的最常见终端.于是,网页设计师不得不面对一个难题:如何才能在不同大小 ...

  8. jQuery Lint: enables you to automatically inject jQuery Lint into the page as it is loaded (great for ad-hoc code validation)

    FireQuery is a Firebug extension for jQuery development jQuery Lint: enables you to automatically in ...

  9. navicat重新系统丢失libmysql_e

    解决方法: 1把libmysql_e拷贝到c盘的Windows的system文件夹

  10. 使用LAMP创建基于wordpress的个从博客站点

    參考: http://blog.csdn.net/ck_boss/article/details/27866117 一.mysql配置 1.安装mysql yum install mysql-serv ...