zoukankan      html  css  js  c++  java
  • HDU 472 Hamming Distance (随机数)

    Hamming Distance

    Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
    Total Submission(s): 1127 Accepted Submission(s): 425


    Problem Description
    (From wikipedia) For binary strings a and b the Hamming distance is equal to the number of ones in a XOR b. For calculating Hamming distance between two strings a and b, they must have equal length.
    Now given N different binary strings, please calculate the minimum Hamming distance between every pair of strings.
     
    Input
    The first line of the input is an integer T, the number of test cases.(0<T<=20) Then T test case followed. The first line of each test case is an integer N (2<=N<=100000), the number of different binary strings. Then N lines followed, each of the next N line is a string consist of five characters. Each character is '0'-'9' or 'A'-'F', it represents the hexadecimal code of the binary string. For example, the hexadecimal code "12345" represents binary string "00010010001101000101".
     
    Output
    For each test case, output the minimum Hamming distance between every pair of strings.
     
    Sample Input
    2 2 12345 54321 4 12345 6789A BCDEF 0137F
     
    Sample Output
    6 7
     

    题意:就是任意两个16进制字符串XOR,在 所有结果中,求结果以二进制表示时,含1个数最少是多少。

    import java.io.*;
    import java.util.*;
    public class Main {
    	BufferedReader bu;
    	PrintWriter pw;
    	int t,n,min;
    	public static void main(String[] args) throws IOException{
    		new Main().work();
    	}
    	void work() throws IOException{
    		bu=new BufferedReader(new InputStreamReader(System.in));
    		pw=new PrintWriter(new OutputStreamWriter(System.out),true);
    		t=Integer.parseInt(bu.readLine());
    		while(t--!=0){
    			n=Integer.parseInt(bu.readLine());
    			String str[]=new String[n];
    			for(int i=0;i<n;i++){
    				str[i]=bu.readLine();
    			}
    			min=0xfffffff;
    			Random da=new Random(1);
    			for(int i=1;i<1000000;i++){
    				int x=da.nextInt()%n;
    				int y=da.nextInt()%n;
    				
    				if(x!=y&&x>=0&&y>=0){
    					int num1=Integer.parseInt(str[x],16);
    					int num2=Integer.parseInt(str[y],16);
    					int num=num1^num2;
    					getCount(num);
    				}
    			}
    			pw.println(min);
    		}
    	}
    	void getCount(int num){
    		int len=0;
    		int count=0;
    		while((num>=(1<<len))){
    			if((num&(1<<len))!=0){
    				count++;
    			}
    			len++;
    		}
    		min=Math.min(min,count);
    		
    	}
    }
    


     

  • 相关阅读:
    MWC飞控增加声纳定高的方法(转)
    c语言字符串分割函数(转)
    移动端IM系统的协议选型:UDP还是TCP?(转)
    如何编写Linux设备驱动程序(转)
    TCP连接探测中的Keepalive和心跳包(转)
    为什么说基于TCP的移动端IM仍然需要心跳保活?(转)
    基于 FPGA 的图像边缘检测(转)
    NTC热敏电阻基础以及应用和选择(转)
    通用CRC32校验程序,可完美匹配STM32硬件CRC算法(转)
    MAX31855 热电偶至数字输出转换器
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3318004.html
Copyright © 2011-2022 走看看