海明校验码具有检测错误的能力,同时还具有找出错误所在位置的能力。海明码的编码原理:给出信息,组成信息位:

海明码的生成_校验码

,其余位置填充位置码,海明码由此组成。


给定信息

海明码的生成_校验码_02

. 构建海明码:


海明码的生成_i++_03


其中,pi的值由和bi的位置关系决定。


我们称pi是校验位,bi是信息位。


3=1+2 ——>b1


5=1+4 ——>b2


6=2+4 ——>b3


7=1+2+4 ——>b4


那么:


海明码的生成_#include_04



hunnu OJ 10627

海明编码


​http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=10627​

给出信息码,求出每一组的测试海明码

#include <stdio.h>
#include <string.h>
using namespace std;
const int N=205;
char str[N];
int bit[N];
bool vis[N];
int main()
{
for(int i=0;i<8;i++){
vis[1<<i]=1;
}
int n,cas=1;
scanf("%d",&n);
while(n--){
scanf("%s",str);
memset(bit,0,sizeof(bit));
int len=strlen(str);
for(int i=0,dex=3;i<len;i++){
int pot=0;
while(dex>=(1<<pot)){
if(dex&(1<<pot)) {
bit[1<<pot]^=(str[i]-'0');
}
pot++;
}
//show();
dex++;
if(vis[dex]) dex++;
}
printf("CASE:%d\n",cas++);
int d=0;
for(int i=1;i<N;i++){
if(vis[i]) printf("%d",bit[i]);
else printf("%c",str[d++]);
if(d==len) break;
}
puts("");
}
return 0;
}