Hamming Codes

链接

分析:码农模拟题,首先我们先求出对于0来说满足条件的数,然后在从集合当中筛选出任意两个都满足条件的数即可

  1 /*
  2     PROB:hamming
  3     ID:wanghan
  4     LANG:C++
  5 */
  6 #include "iostream"
  7 #include "cstdio"
  8 #include "cstring"
  9 #include "string"
 10 #include "vector"
 11 #include "cmath"
 12 #include "algorithm"
 13 using namespace std;
 14 const int maxn=1000+10;
 15 int N,B,D;
 16 vector<int> p;
 17 int vis[maxn];
 18 string transform(int x,int y,string s)  
 19 {  
 20     string res="";  
 21     int sum=0;  
 22     for(int i=0;i<s.length();++i)  
 23     {  
 24         if(s[i]=='-')  continue;  
 25         if(s[i]>='0'&&s[i]<='9')  
 26         {  
 27             sum=sum*x+s[i]-'0';  
 28         }  
 29         else  
 30         {  
 31             sum=sum*x+s[i]-'A'+10;  
 32         }  
 33     }  
 34     while(sum)  
 35     {  
 36         char tmp=sum%y;  
 37         sum/=y;  
 38         if(tmp<=9)  
 39         {  
 40             tmp+='0';  
 41         }  
 42         else  
 43         {  
 44             tmp=tmp-10+'A';  
 45         }  
 46         res=tmp+res;  
 47     }  
 48     if(res.length()==0)  res="0";  
 49     if(s[0]=='-')  res='-'+res;  
 50     return res;  
 51 }
 52 string change(int x){
 53     string res="";
 54     while(x){
 55         int num=x%10;
 56         res+=num+'0';
 57         x/=10;
 58     }
 59     int i=0,j=res.length()-1;
 60     while(i<j){
 61         swap(res[i],res[j]);
 62         i++,j--;
 63     }
 64     return res;
 65 }
 66 bool solve(int a,int b){
 67     string s1=transform(10,2,change(a)),s2=transform(10,2,change(b));
 68     string res1="",res2="";
 69     int cha;
 70     if(s1.length()<B){
 71         cha=B-s1.length();
 72         for(int i=0;i<cha;i++)
 73             res1+='0';
 74     }
 75     res1+=s1;
 76     if(s2.length()<B){
 77         cha=B-s2.length();
 78         for(int i=0;i<cha;i++)
 79             res2+='0';
 80     }
 81     res2+=s2;
 82     int cnt=0;
 83     for(int i=0;i<B;i++){
 84         if(res1[i]!=res2[i]){
 85             cnt++;
 86         }
 87     }
 88     if(cnt>=D)
 89         return true;
 90     return false;
 91 }
 92 struct Node{
 93     int x;
 94     string res;
 95 };
 96 bool cmp(Node a,Node b){
 97     return a.res<b.res;
 98 }
 99 int main()
100 {
101     freopen("hamming.in","r",stdin);
102     freopen("hamming.out","w",stdout);
103     cin>>N>>B>>D;
104     p.push_back(0);
105     int pos=0;
106     int i=1;
107     int num=pow(2,B+1)-1;
108     while(i<num){
109         if(solve(pos,i)){
110             p.push_back(i);
111         }
112         i++;
113     }
114     memset(vis,0,sizeof(vis));
115     int len=p.size();
116     for(int i=0;i<len;i++){
117         for(int j=i+1;j<len;j++){
118             if(!vis[i]){
119                 if(vis[j]||!solve(p[i],p[j])){
120                     vis[j]=1;
121                 }
122             }
123         }
124     }
125     vector<int> f;
126     for(int i=0;i<len;i++){
127         if(!vis[i]){
128             int zz=p[i];
129             f.push_back(zz);
130         }
131     }
132     for(int i=0;i<N;i++){
133         if(i%10==9){
134             cout<<f[i]<<endl;
135         }else{
136             if(i==N-1)
137                 cout<<f[i]<<endl;
138             else
139                 cout<<f[i]<<" ";
140         }
141     }
142     return 0;
143 }
View Code

 

posted @ 2017-07-11 09:04  wolf940509  阅读(104)  评论(0编辑  收藏  举报