gym 101170 NWERC 2016 H Hamiltonian Hypercube

来源:互联网 发布:淘宝店铺上架宝贝数量 编辑:程序博客网 时间:2024/06/01 10:40

Problem

Northwestern European Regional Contest 2016
vjudge.net/problem/Gym-101170H

Meaning

给出两个 n 位的格雷码,问它们只间隔者多少个码字(code word)

Analysis

格雷码只是 另一种编码方式,所以相当于问两个整数之间夹着多少个整数。将它转换成原码(得到真值)就很好处理。

  • 原码转格雷码
int to_gray(int x){    return x ^= x >> 1;}
  • 格雷码转原码
int to_binary(int x){    int y = x;    while(x >>= 1)        y ^= x;    return y;}

Code

#include <cstdio>#include <algorithm>using namespace std;const int N = 60;char a[N+2], b[N+2];long long trans(char *s){    long long t = 0;    for( ; *s; ++s)        t = t << 1 | *s - '0'; // t = t * 2 + *s - '0'    return t;}long long to_bin(long long g){    long long x = g;    while(g >>= 1)        x ^= g;    return x;}int main(){    scanf("%*d %s %s", a, b);    long long x = to_bin(trans(a)), y = to_bin(trans(b));    printf("%lld\n", max(x-y, y-x) - 1);    return 0;}