首页 > 代码库 > hdu 4712 随机数方法求解

hdu 4712 随机数方法求解

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<time.h>

using namespace std;

const int Max = 100050;

char str[Max][10];
int mark[16][16];

int num[] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
			//0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
int charToint(char ch)
{
	if(ch >= '0' && ch <= '9') return ch - '0';
	return ch - 'A' + 10; 
}

void getMark()
{
	int t;
	for(int i = 0; i < 16; i ++)
	{
		for(int j = 0; j < 16; j ++)
		{
			t = i ^ j;
			mark[i][j] = mark[j][i] = num[t]; 
		}
	}	
}

int getMin(int xi, int yi)
{
	int a, b, sum = 0;
	for(int i = 0; i < 5; i ++)
	{
		a = charToint(str[xi][i]);
		b = charToint(str[yi][i]);
		sum += mark[a][b];
		//cout<<a<<' '<<b<<" = "<<mark[a][b]<<endl;
	}
	//cout<<"sum = "<<sum<<endl;
	return sum; 
}

int main()
{
	int T, N, ansmin;
	
	getMark();
	
	scanf("%d", &T);
	while(T --)
	{
		ansmin = 100;
		
		scanf("%d", &N);
		for(int i = 0; i < N; i ++)
		{
			scanf("%s", str[i]); 
		}
		for(int i = 0; i < 900000; i ++)
		{
			int x = rand() % N;
			int y = rand() % N;
			if(x == y) continue;
			int temp = getMin(x, y);
			if(temp < ansmin) ansmin = temp;
		}
		printf("%d\n", ansmin);
	}
	
	return 0;
}