Hamming Problem Solving Report
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Topic
Find n binary number, each number has b bit, so that the number of 22 "Hamming distance" is at least d. (If there are multiple sets of solutions, the output dictionary order is minimal.) )
Hamming distance refers to: the number of different bits of two binary numbers.
"Data Range"
1<=n<=64
1<=b<=8
1<=d<=7
"Input Format"
A row of three numbers n, B, D, Space split.
"Output Format"
Output from small to large, output 10 numbers per line.
"Input Sample"
16 7 3
"Output Example"
0 7 25 30 42 45 51 52 75 76
82 85 97 102 120 127
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Analysis
Another enumeration, greedy.
Up to 256 numbers, starting from 0 sweep (small to large), can take, and all the distance is less than the number of D "deleted." In this way, only the optimal solution can be taken. Because there is no solution to the problem (the topic has not said no solution to do, of course, there are solutions.) ), and the time to delete is 256, so the total time is slightly larger than 62500, Changshu is about 8, fully acceptable.
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Summary
AC for the second time.
The first commit did not, forget the Usaco required output end of line wrapping.
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Code
1 /*2 id:icedrea13 prob:hamming4 lang:c++5 */6 7#include <iostream>8#include <fstream>9 using namespacestd;Ten One voidDtob (intnum) A { - intbit[Ten],l=0; - while(num) {bit[l++]=num&1; num>>=1; } the for(inti=l;i<7; ++i) cout<<0; - for(inti=l-1; i>=0;-I.) cout<<Bit[i]; - } - voidLook () + { -Ifstreaminch("Hamming.out"); + A intnum; at while(inch>>num) - { - Dtob (num); -cout<<Endl; - } - in inch. Close (); - } to + //just a look----------------------------------------------------------- - //go~! the * intn,b,d; $ BOOLhave[ the];Panax Notoginseng - intnum[ -],sum; the + intDisintXinty) A { the ints=0; + while(x| | Y) {s+= (x^y) &1; x>>=1; y>>=1; } - returns; $ } $ - voidDelintx) - { the for(inty=0;y< (1<<B); + +y) - if(Dis (x, y) <d) have[y]=false;Wuyi } the - voidPrint (Ostream & out) Wu { - for(intI=0; i<sum-1; ++i) out<<num[i]<< (i%Ten==9?"\ n":" "); About out<<num[sum-1]<<Endl; $ } - - intMain () - { A //Look (); return 0; +Ifstreaminch("hamming.in"); theOfstream out("Hamming.out"); - $ inch>>N>>B>>D; the for(intx=0;x< (1<<B); ++x) have[x]=true; the for(intx=0;x< (1<<B) && sum<n;++x) the { the if(!have[x])Continue; -num[sum++]=x; del (x); in } the the if(sum<n) out<<"Not enough!"<<endl;ElsePrint out); About the inch. Close (); the out. Close (); the return 0; +}
Usaco Section2.1 Hamming Codes Problem Solving report "Icedream61"