HDU 4712Hamming Distance(隨機函式運用)
Hamming Distance
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 1043 Accepted Submission(s): 394
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.
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
Source
題目大意:給你很多串,最多10^5個串,每個串長度是5,16進位制轉化為2進位制,問你任意兩個串抑或得到1的最小的個數。
解題思路:做題的時候只顧著如何降低複雜度,簡單的O(n^2)肯定會超時,最後才得知是隨機函式寫。結果只可能是0~20.自己寫的時候10W次還是WA了,人品不行啊,果斷隨機100W次A了。
不過聽說可以更暴力地直接列舉最前面的10000個也可以A。網路賽要敢於嘗試。
題目地址:Hamming Distance
AC程式碼:
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
int a[100005];
int main()
{
int tes,i,j,k,res,ans;
scanf("%d",&tes);
while(tes--)
{
int n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%X",&a[i]); //16進位制讀取
res=20; //結果初始為最大20
for(i=1;i<=1000000;i++)
{
j=rand()%n; //隨機函式
k=rand()%n;
if(j==k)
continue;
ans=0;
int tmp=a[j]^a[k]; //抑或
while(tmp) //抑或算1的個數,儲存到ans中
{
if(tmp&1)
ans++;
tmp>>=1;
}
if(ans<res)
res=ans;
}
cout<<res<<endl;
}
return 0;
}
//2453MS 676K
相關文章
- Oracle 隨機函式Oracle隨機函式
- (轉)Oracle隨機函式Oracle隨機函式
- Oracle取隨機數函式Oracle隨機函式
- Oracle隨機函式的取法Oracle隨機函式
- openresty及lua的隨機函式REST隨機函式
- AS使用Random函式建立隨機數random函式隨機
- 隨機向量特徵函式的求法隨機特徵函式
- 運用JS 實現隨機點名 (隨機點名)JS隨機
- HDU 3501 Calculation 2 (尤拉函式應用)函式
- SG 函式初步 HDU 1536 && HDU 1944函式
- 隨機變數函式的分佈隨機變數函式
- javascript隨機數函式Math.random()JavaScript隨機函式random
- awk之隨機函式rand()和srand()隨機函式
- 隨機函式DBMS_RANDOM.STRING隨機函式random
- HDU 1709 The Balance(母函式)函式
- HDU2588GCD(尤拉函式)GC函式
- Python隨機函式random使用詳解Python隨機函式random
- 概率論08 隨機變數的函式隨機變數函式
- Matlab產生隨機數函式小結Matlab隨機函式
- java Math數學工具及Random隨機函式Javarandom隨機函式
- C語言生成隨機數函式的用法C語言隨機函式
- Oracle中生成隨機數的函式(轉載)Oracle隨機函式
- HDU1729 Stone Game (SG函式)GAM函式
- php 隨機顯示圖片的函式程式碼PHP隨機函式
- C++ 中隨機函式 rand() 和 srand() 的用法C++隨機函式
- 【matlab程式設計】matlab隨機數函式Matlab程式設計隨機函式
- 使用Oracle分析函式隨機抽取N條記錄Oracle函式隨機
- MySQL建立隨機生成電話的儲存函式MySql隨機儲存函式
- HDU 4002Find the maximum(尤拉函式)函式
- Python 內嵌函式運用(探究模組)Python函式
- 學習並運用JavaScript的原生函式JavaScript函式
- 運用inlinehook主動呼叫函式inlineHook函式
- (譯)函式式元件在Vue.js中的運用函式元件Vue.js
- matlab中的產生隨機數的rand函式Matlab隨機函式
- Excel2007RANDBETWEEN隨機數函式Excel隨機函式
- MATLAB中常用的幾種隨機數生成函式Matlab隨機函式
- Oracle 函式大全(字串函式,數學函式,日期函式,邏輯運算函式,其他函式)Oracle函式字串
- Java學習隨筆(函式式介面)Java函式