Usaco Section2.1 Hamming Codes Problem Solving report "Icedream61"

Source: Internet
Author: User

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&LT;&LT;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&LT;&LT;B); ++x) have[x]=true; the      for(intx=0;x< (1&LT;&LT;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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.